Click to See Complete Forum and Search --> : structure as a member of class


raju sungar
September 5th, 2002, 04:51 AM
Hi all,
How to use structures as a class member in C++

e.g
class A
{
public:
struct b
{
int x
};
void display()
{ cout<<"Hi"}
};

PaulWendt
September 5th, 2002, 06:45 AM
Having a structure [or class] embedded inside your class is fine;
you need to actually instantiate an object of that struct, though.
Think of a class [or struct] as a cookie cutter; you need to actually
create the cookie [class/struct] from that cookie cutter.

--Paul

kuphryn
September 5th, 2002, 03:04 PM
Yes, you can nest a struct and/or class inside a class.


class X
{
public:
struct Y
{
...
};

class Z
{
...
};

private:
Y m_MyStruct;
Z m_nMyClass;
};


Kuphryn

raju sungar
September 5th, 2002, 11:53 PM
Hi all
class base
{

public:
struct Y
{
int a;
};

struct Z
{
int b;
};

private:
Y m_MyStruct;
Z m_nMyClass;
};
HOw to define constructor for this class?

thanks
RAJU

jfaust
September 6th, 2002, 12:30 AM
Y and Z are distinct classes and can have constructors of their own, if they need them. Also base can have a constructor. There's nothing special about any of these constructors.

Note that "class A { public: };" and "struct A {};" are equivalent. A struct is a class.

Jeff