if i have class CAnalyzer, and i want to make a pointer from this class to new class name CManager. how can i do it? note: i need to make the pointer inside init func in CAnalyzer. thanks
-
If you have a class CAnalyzer, you are using the icky Microsoft naming convention where every class starts with a 'C'. If you decide to change it from a class to a structure, you will have to change CAnalyzer to SAnalyzer in every file that references CAnalyzer. Better not to encode data types in names.Thomas Matthews– Thomas Matthews2011-01-06 20:40:32 +00:00Commented Jan 6, 2011 at 20:40
-
@Thomas: "class" is not a data type.ltjax– ltjax2011-03-18 11:59:11 +00:00Commented Mar 18, 2011 at 11:59
-
@Itjax: please state your reference. "With object-oriented programming, a programmer can create new data types to meet application needs. Such an exercise as known as "data abstraction" and the result is a new class of data.", searchsoa.techtarget.com/definition/data-typeThomas Matthews– Thomas Matthews2011-03-18 18:46:12 +00:00Commented Mar 18, 2011 at 18:46
-
@Thomas: I'm talking about the keyword "class" (hence no article) which is not a data-type, but an identifier that a data-type definition or declaration will follow. A class (now with an article, hence something that you defined using "class") however is a data-type (which is what you're quoting) - but the class surely doesn't have a data-type (it has a meta-type, which is "class").ltjax– ltjax2011-03-18 19:13:07 +00:00Commented Mar 18, 2011 at 19:13
Add a comment
|
3 Answers
class CManager; // Forward declaration (may not be needed)
class CAnalyzer
{
// Other stuff goes here
private:
CManager *p_manager;
};
I don't understand what you mean by "I need to make the pointer inside init func in CAnalyzer".
1 Comment
John Dibling
+1 I wasnt sure either, but your guess might be more right than mine
I'm not sure I understand exactly what you're asking, so I will guess.
You want to write a member function of CAnalyzer that returns a pointer to a new instance of a CManager? You can do that like this:
CManager* CAnalyzer::CreateManager()
{
return new CManager;
}
You should, however, use smart pointers rather than raw pointers in the interest of robust programming.
2 Comments
Puppy
Shouldn't that be CreateManager?
John Dibling
@DeadMG: Yeah, probably. Fixed