Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

howto: private classes in classes 1

Status
Not open for further replies.

AMosmann

Programmer
May 27, 2003
82
DE
How can I declare a private class in another class,
f.e.
class clo{

private:
class cli{
long mi;
}
cli i1;
...
void f(cli *x);
}

clo::f(cli *x){
x.mi=0815;
}


Greetings Andreas
 
i would assume the code you posted doesnt work. usually you would do something like this

#include "Die.h"

//define class PairOfDice
class PairOfDice
{
public:
PairOfDice();
void rollDice(int[]);

private:
Die dieOne;
Die dieTwo;
};
but if Die class is private that probly wont work. im curious to see what others say.
 
I have a very large object and to keep the interface as small as possible I would like to put the declaration of some classes that only have a meaning inside of the large object in the private section.

So I suppose to make the object better usable.

The project the object is to put in is a big one too and so I dont want to make it needlessly complicated (name conflicts, large interface...)

If there is anyone who can help it would be great



Greetings Andreas
 
I found my mistake.

the above code should work.
the mistake was a muddle between 2 functions.
I put the code I wanted to stick into a function into another one, and the compiler told me: I dont know this class...

Sorry for the trouble ...



Greetings Andreas
 
On the topic of keeping the interface small:

Since the private class is actually an internal thingie you could also consider declaring it in the implementation (.cpp).
In the interface class it is referred to by pointer. Then the only implact on the interface would be a forward declaration.

Sample:

---- .h ----
// Forvard decl.
class MyInternal;

class MyInterface
{
public:
MyInterface();
~MyInterface();
// Some stuff
private:
MyInternal* mInternal;
};

---- .cpp ----

class MyInternal
{
// Some stuff
};

MyInterface::MyInterface():mInternal(new MyInternal)
{

}

MyInterface::~MyInterface()
{
delete mInternal;
mInternal = 0;
}



Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
thx, good idea, cause I get a problem with too long names when I use std::list
(std::list <MyClass::MyInternalClass,...)
Think it will shift this problem to later times :)



Greetings Andreas
 
to bee continued ...
can you answer the following question:

I need this MyInternalClass only inside the private functions
If I forward &quot;class MyInternalClass;&quot; outside MyClass everyone knows that I use this
If I forward it inside of MyClass the names are too long...

What can I do not to show the internal class and not to get this compiler warning about too long names (without disable warning and without showing the internal class), is there a way (maybe I can change compiler setup to use longer names...)

thanx

Greetings Andreas
 
>If I forward &quot;class MyInternalClass;&quot; outside MyClass everyone knows that I use this

So what? They won't know what it looks like (and won't be able to use it). I don't think you should be too paranoid about this :)


Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
You are right, I did it like you said.
But it can not be bad to know the other ways ...

:) I am paranoid :) (-;

Greetings Andreas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top