DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   inherit class from structure (http://www.daniweb.com/forums/thread170042.html)

kv123 Jan 21st, 2009 10:10 am
inherit class from structure
 
Hi,

Is this valid in C++??

struct A{...};

class B: public A{...};

Thanks!

Comatose Jan 21st, 2009 12:44 pm
Re: inherit class from structure
 
In C++ A Struct IS a Class. The only difference is that in a class, the members by default are private. In a Struct, they are by default public.

richard bach Oct 21st, 2009 9:07 am
class base
{ protected:
struct node{};
};
class derived :public base
{
struct node *prev;
};

when i do this i 'm getting an error that prev is not a member of base::node.
what can i do if i want to declare an additional variable
of struct node type in class derived

YousefAB Oct 26th, 2009 10:00 am
richard bach, when I compile exactly what you have, i don't get a problem.
Using Visual Studio 2008. V.9.021022.8

#include <iostream>

using namespace std;


class base
{ protected:
struct node{};
};
class derived :public base
{
struct node *prev;
};


class A
{
  public:
      int a;
};

struct B : A { };

struct C
{
public:
      int c;
};

class D : public C { };

int main()
{
  B b;
  D d;
  b.a = 1;
  d.c = 2;
  cout << b.a << " " << d.c << endl;
}

I compile that and no errors, and I do actually get the numbers.
I don't know to what extend this works, but to this, it is working.


All times are GMT -4. The time now is 8:30 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC