Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Function return class object

Posted on 2006-10-22
4
Medium Priority
?
315 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

972 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