Solved

Function return class object

Posted on 2006-10-22
4
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 17784845
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
ID: 17785021
Thank you jkr.

This has been killing me for 4 days now
0
 

Author Comment

by:i_dont_have_a_name
ID: 17816936
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
ID: 17816995
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

631 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