Passing data in constructors

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster

Passing data in constructors

 
0
  #1
Aug 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Passing data in constructors

 
0
  #2
Aug 15th, 2008
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 483 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC