Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Function return class object

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 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