Solved

Function return class object

Posted on 2006-10-22
4
267 Views
Last Modified: 2010-04-01
I have a class called event that has a constructor where i send it a long, 3 ints, and a const char [] now this constructor works when i use it in  main but for this program i have to have a function that returns an event object but when i try to do this my char []  does not come out right

this is what i do inside the main
event appointment(1190,"Doc",10,2,2006);  // Works
event appointment2 = makeEvent(1190,"Doc",10,2,2006); // Does not work ( the char array prints  α!E )


here is the function
event makeEvent(long s, const char n[], int d, int m, int y)
{
      event newEvent(s,n,d,m,y);
      return newEvent;
}
0
Comment
Question by:i_dont_have_a_name
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
Comment Utility
Your class needs a copy constructor, otherwise the 'char*' member will point to invalid memory after the function exits. Just add one:

class event {

public:

//...

event (const event& r) {

s= r.s;

strcpy(n,r.n);

d = r.d;

m = r.m;

y = r.y;

};

//...

};

See also http://en.wikipedia.org/wiki/Copy_constructor
0
 

Author Comment

by:i_dont_have_a_name
Comment Utility
Thank you jkr.

This has been killing me for 4 days now
0
 

Author Comment

by:i_dont_have_a_name
Comment Utility
I got my program to work thanks to jkr but i have a question about the code

I thought that if the data is private that you cant access it through the dot operator ex. in my program i have a char *desc under private but in the copy constructor i use strcpy(desc,copy.desc) and that works so why does this work?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
No, 'private' does not apply to access from within the same class scope. Both objects are of the same class, that's why it works.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now