Solved

Function return class object

Posted on 2006-10-22
4
304 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 126
Safe conversion? 4 87
Need some help with mailto 16 44
I could not set window to top 4 63
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

751 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