Problems with allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    #1

    Problems with allocation

    Hello

    I have a problem. The following program throws a "0" instead of a "25". I´m from the JAVA world, and in JAVA the output is "25"

    Hope, anybody can help me

    Thanks ..


    class Content

    public

    Content()
    wert = 0

    Content::~Conte nt()


    int getValue()
    return value

    void setValue(int _value)
    value= _value


    private
    int value
    }

    --

    #include "Content.h

    class Container

    public

    Container()

    Container::~Con tainer()


    Content getContent()
    return content

    void setContent(Cont ent _content)
    content = _content


    private
    Content content
    }

    --

    // Allocationprobl em.cp
    /

    #include "stdafx.h
    #include <iostream.h
    #include "Container. h

    int main(int argc, char* argv[])

    Container container = Container()
    container.getCo ntent().setValu e(25)
    cout << container.getCo ntent().getValu e() << endl

    return 0


  • Stu Smith

    #2
    Re: Problems with allocation

    You need to either return your content object by reference (or pointer), or
    supply a copy constructor.
    I'd recommend doing both, plus defining an assignment operator.


    "Michael" <anonymous@disc ussions.microso ft.com> wrote in message
    news:73E9E283-FB1E-46D1-ACDB-8F239EE9987B@mi crosoft.com...[color=blue]
    > Hello,
    >
    > I have a problem. The following program throws a "0" instead of a "25".[/color]
    I´m from the JAVA world, and in JAVA the output is "25".[color=blue]
    >
    > Hope, anybody can help me.
    >
    > Thanks ...
    >
    >
    >
    > class Content{
    >
    > public:
    >
    > Content(){
    > wert = 0;
    > }
    > Content::~Conte nt(){
    > }
    >
    > int getValue(){
    > return value;
    > }
    > void setValue(int _value){
    > value= _value;
    > }
    >
    > private:
    > int value;
    > };
    >
    > ---
    >
    > #include "Content.h"
    >
    > class Container {
    >
    > public:
    >
    > Container(){
    > }
    > Container::~Con tainer(){
    > }
    >
    > Content getContent(){
    > return content;
    > }
    > void setContent(Cont ent _content){
    > content = _content;
    > }
    >
    > private:
    > Content content;
    > };
    >
    > ---
    >
    > // Allocationprobl em.cpp
    > //
    >
    > #include "stdafx.h"
    > #include <iostream.h>
    > #include "Container. h"
    >
    > int main(int argc, char* argv[]){
    >
    > Container container = Container();
    > container.getCo ntent().setValu e(25);
    > cout << container.getCo ntent().getValu e() << endl;
    >
    > return 0;
    > }
    >[/color]


    Comment

    Working...