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"}
};
Printable View
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"}
};
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
Yes, you can nest a struct and/or class inside a class.
KuphrynCode:class X
{
public:
struct Y
{
...
};
class Z
{
...
};
private:
Y m_MyStruct;
Z m_nMyClass;
};
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
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