| | |
Passing data in constructors
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 1
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
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.
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?
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?
![]() |
Similar Threads
- urgent help ith c++ program (C++)
- urgent help with c++ function access(/cpp) (C++)
- void pointers (C++)
- help with parrallel arrays (C++)
- Zend PHP Certification (PHP)
Other Threads in the C++ Forum
- Previous Thread: Pls help me with data input
- Next Thread: Dev-C++ compiler with Netbeans IDE
Views: 483 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






