Problems with allocation

G

Guest

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::~Content()


int getValue()
return value

void setValue(int _value)
value= _value


private
int value
}

--

#include "Content.h

class Container

public

Container()

Container::~Container()


Content getContent()
return content

void setContent(Content _content)
content = _content


private
Content content
}

--

// Allocationproblem.cp
/

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

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

Container container = Container()
container.getContent().setValue(25)
cout << container.getContent().getValue() << endl

return 0
 
S

Stu Smith

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 said:
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::~Content(){
}

int getValue(){
return value;
}
void setValue(int _value){
value= _value;
}

private:
int value;
};

---

#include "Content.h"

class Container {

public:

Container(){
}
Container::~Container(){
}

Content getContent(){
return content;
}
void setContent(Content _content){
content = _content;
}

private:
Content content;
};

---

// Allocationproblem.cpp
//

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

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

Container container = Container();
container.getContent().setValue(25);
cout << container.getContent().getValue() << endl;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top