Archived

This topic is now archived and is closed to further replies.

Drakkcon

Nested Classes/Sub-Classes in C++

Recommended Posts

How do you nest classes in C++. Why doesn''t my code work?:
#include <iostream>

using namespace std;

class foo
{
 public:
 class jimbo
 {
  public:
  int wassup = 74;
 };
};

foo w;

int main()
{
 cout << w.jimbo.wassup;
 return 0;
}
The true general first seeks victory, then seeks battle - Sun Tzu

Share this post


Link to post
Share on other sites
Okay, your "class foo { ... };" describes a foo class, but it does not create a foo object. To do that you had to create one with "foo w;" Similarly, your jimbo class in foo is being defined (accessable via foo::jimbo), but there is no actual object of type "jimbo" inside the foo class.

Share this post


Link to post
Share on other sites
Ohhh, so they should be separate. Like this:

#include <iostream>
using namespace std;

struct jimbo //I''m too lazy to type public

{
int wassup = 47;
};

struct foo //might as well be struct

{
jimbo nested;
}parent;

int main()
{
cout << parent.nested.wassup;
return 0;
}

Share this post


Link to post
Share on other sites
meh. Oh, and thanx for the clear up - time for my inner OO control freak to manifest! May there be constructers everywhere!(evil laugh).

Share this post


Link to post
Share on other sites
it doesn't work:

error:

g++.exe -D__DEBUG__ -c Main.cpp -o Main.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" -I"C:/DX9bSDK/Include" -ansi -fsave-memoized -O3 -pg -g3

C:/Dev-Cpp/include/c++/bits/locale_facets.tcc: In function `int
std::__convert_from_v(char*, int, const char*, _Tv, int* const&, int) [with
_Tv = long unsigned int
]':
C:/Dev-Cpp/include/c++/bits/locale_facets.tcc:720: instantiated from `_OutIter std::num_put<_CharT, _OutIter>::_M_convert_int(_OutIter, std::ios_base&, _CharT, char, char, _ValueT) const [with _ValueT = long unsigned int, _CharT = char, _OutIter = std::ostreambuf_iterator<char, std::char_traits<char> >]'
C:/Dev-Cpp/include/c++/bits/locale_facets.tcc:899: instantiated from `_OutIter std::num_put<_CharT, _OutIter>::do_put(_OutIter, std::ios_base&, _CharT, long unsigned int) const [with _CharT = char, _OutIter = std::ostreambuf_iterator<char, std::char_traits<char> >]'
C:/Dev-Cpp/include/c++/bits/locale_facets.h:745: instantiated from `_OutIter std::num_put<_CharT, _OutIter>::Put(_OutIter, std::ios_base&, _CharT, long unsigned int) const [with _CharT = char, _OutIter = std::ostreambuf_iterator<char, std::char_traits<char> >]'
C:/Dev-Cpp/include/c++/bits/ostream.tcc:215: instantiated from `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]'
C:/Dev-Cpp/include/c++/ostream:122: instantiated from `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]'
Main.cpp:19: instantiated from here
C:/Dev-Cpp/include/c++/bits/locale_facets.tcc:2070: `strdup' undeclared (first
use this function)

C:/Dev-Cpp/include/c++/bits/locale_facets.tcc:2070: (Each undeclared identifier
is reported only once for each function it appears in.)

make.exe: *** [Main.o] Error 1

Execution terminated



#include <iostream>

using namespace std;

class A
{
public:

class B
{
public:
int foo;
} nestedB;
}whatever;

int main()
{
whatever.nestedB.foo = 400;
cout << whatever.nestedB.foo;
return 0;
}


It IS a C++ project, and I can think of nothing else.

EDIT: It's a bug in MinGW and Dev-C++.

[edited by - Drakkcon on May 19, 2004 7:43:54 AM]

edit: breaking tables

[edited by - SiCrane on May 19, 2004 5:14:19 PM]

Share this post


Link to post
Share on other sites
struct jimbo
{
int wassup = 47;
};

Is not valid C++ code. You can't initialize an non-const non-static datamember like that. You'd have to either initialize the the value in the constructor of jimbo, or make wassup a static const datamember. In the case of the latter solution, you'd access the value like jimbo::wassup.

edit: nm you fixed that problem in your last post

[edited by - Polymorphic OOP on May 19, 2004 10:40:28 AM]

Share this post


Link to post
Share on other sites
"Inside() : Y(2) {}" is just a form of constructor. It is exactly identical to saying:

Inside () { Y=2; }

It''s just putting the initializers outside of the function. This is better than initializing member variables inside the constructor but I can''t remember why...

Instead of trying to conjecture on it, I''ll just let someone else explain why it''s better.

-Auron

Share this post


Link to post
Share on other sites
Hi,

You created object to the 'foo' class and trying to access the nested class public variable. But nested class will be visible within the 'foo' class. We can access nested class memeber variables, or member functions by creating object in 'foo' class. Normally these nested class objects used pass as a param. in the main class methods. I think this will help you.

Ravi

Share this post


Link to post
Share on other sites