| | |
about reference of a instance in class
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 0
I want to disign a class, which can include a reference of a instance. But the class of this instance is not confirm. In java I can use "object" class, because "object" class is father class of all class. for example
class XX{
...
...
private object YY;
...
public void setYY(object y){YY = y}
public object getYY(){return YY}
}
use setYY I can give XX a reference of a instance, and use getYY to receive it. I dont need care of the class of this instance, because object is "TOP-Father-class".
But in C++ how can I do that?
class XX{
...
...
private object YY;
...
public void setYY(object y){YY = y}
public object getYY(){return YY}
}
use setYY I can give XX a reference of a instance, and use getYY to receive it. I dont need care of the class of this instance, because object is "TOP-Father-class".
But in C++ how can I do that?
Last edited by havec; Mar 6th, 2008 at 5:58 pm.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> In java I can use "object" class, because "object" class is father class of all class.
C programmers use
if object is the father, void* is the grandfather.
the solution in C++ depends on the paradigm that you want to use:
generic programming (typesafe):
object oriented programming (not so typesafe):
have a base class (say object) which all objects which can be put into an XX must inherit from.
to use the object that is retrieved, a cast is required. in C++, this would be a
C programmers use
void* where java programmers use object . you cannot use either without a cast; a wrong cast will result in unpleasant consequences in both. in C, incorrect runtime behaviour (seg fault etc.), in java an exception being thrown.if object is the father, void* is the grandfather.
the solution in C++ depends on the paradigm that you want to use:
generic programming (typesafe):
c++ Syntax (Toggle Plain Text)
template< typename T > class XX { // ... private: T yy ; public: void setYY( const T& y ) { yy = y ; } const T& getYY() const { return yy ; } T& getYY() { return yy ; } // ... };
have a base class (say object) which all objects which can be put into an XX must inherit from.
c++ Syntax (Toggle Plain Text)
struct /* __declspec(novtable) */ object { virtual ~object() = 0 { /* empty */ } }; class XX { // ... private: object* yy ; public: void setYY( object* y ) { yy = y ; } const object*getYY() const { return yy ; } object* getYY() { return yy ; } // ... };
dynamic_cast which may fail at runtime. this would mimic java fairly closely. ![]() |
Other Threads in the C++ Forum
- Previous Thread: istringstream
- Next Thread: Understanding filters - help
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






