943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 615
  • C++ RSS
Aug 15th, 2008
0

Passing data in constructors

Expand Post »
I have two files sampleclass.cpp and sampledataclass.cpp along with sampleclass.h and sampledataclass.h

In sampleclass.cpp in the constructor I give a value to a variable

sampleclass.cpp
sampleclass:sampleclass(){
x = "some string value";
}

In sampleclass.h I create x and an instance of the data class
sampleclass.h
CString x;
sampledataclass myDC;

Now in the constructor of the data class I want to be able to set the value of another string to the value of x
sampledataclass.cpp
sampledataclass:sampledataclass(){
CString y = mysampleclass->x;
}

The h file has this
sampleclass.h
sampleclass *mysampleclass;

How do I do this without getting a runtime error
Last edited by kinger29; Aug 15th, 2008 at 1:49 pm.
Similar Threads
Reputation Points: 11
Solved Threads: 2
Light Poster
kinger29 is offline Offline
35 posts
since Mar 2008
Aug 15th, 2008
0

Re: Passing data in constructors

Never define global variables in .h files! You can declare these variables, of course - simply use extern keyword before its type, but define such variables in one and only only cpp file.
As usually, there are better solutions than global variables using, but it's the other story.

The key question in your case: who (and when) must initialize (global?) mysampleclass pointer variable? I don't know the answer because your snippet is too laconic.

It seems you have too cumbersom mechanics for a simple task. May be you present more codes and better your program case description?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pls help me with data input
Next Thread in C++ Forum Timeline: Dev-C++ compiler with Netbeans IDE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC