473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Empty class declarations

Sometimes, when I browse through some code, I see declarations like:

class SomeClass;

These make absolutely no sense to me. Can anyone tell me what these
kind of declarations do?

Thanks!

Jul 23 '05 #1
4 3381
This is forward declaration. When using a class "ClassA" as a member in
another class "ClassB", you will define "ClassB" before "ClassA". But
if "ClassB" also have a member of "ClassA" you have a cycle. You can
solve this problem by using forward declaration.

class ClassA; // forward declaration

class ClassB {
public:
ClassA a;
};

class ClassA {
public:
ClassB b;
};

Jul 23 '05 #2

<jo*********@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
This is forward declaration. When using a class "ClassA" as a member in
another class "ClassB", you will define "ClassB" before "ClassA". But
if "ClassB" also have a member of "ClassA" you have a cycle. You can
solve this problem by using forward declaration.
No, a forward declaration does not solve the situation you
show below, which simply cannot be resolved. (Think about it
for a moment). A forward declaration only allows a pointer
or reference to the forward-declared class to be declared without
its full class definition visible.

class ClassA; // forward declaration

class ClassB {
public:
ClassA a; /* ERROR */
};

class ClassA {
public:
ClassB b;
};


class ClassA;

class ClassB
{
ClassA a; /* ERROR */
ClassA& r; /* OK */
ClassA *p; /* OK */
};

class ClassA
{
};

class ClassC
{
ClassA a; /* OK, full def of ClassA visible */
};

-Mike
Jul 23 '05 #3
Mike Wahler wrote:
class ClassA; // forward declaration

class ClassB {
public:
ClassA a;

/* ERROR */
};

class ClassA {
public:
ClassB b;
};


And just as a side note: If that worked, it wouldn't make much sense to do
it anyway, since an instance of ClassA - as well as an instance of ClassB -
would need infinite storage.

Jul 23 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d6*************@news.t-online.com...
Mike Wahler wrote:
class ClassA; // forward declaration

class ClassB {
public:
ClassA a;

/* ERROR */
};

class ClassA {
public:
ClassB b;
};


And just as a side note: If that worked, it wouldn't make much sense to do
it anyway, since an instance of ClassA - as well as an instance of
ClassB -
would need infinite storage.


That's what I'd hoped to make 'jonas' realize, with
my "think about it for a moment" remark.

-Mike
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris Connett | last post by:
I have an interesting problem with eval(). ---Background Info--- The program I'm working on launches a separate process with a popen to do some highly specialized processing of input, then this...
9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
3
by: Markus Dehmann | last post by:
I have a two different value types with which I want to do similar things: store them in the same vector, stack, etc. Also, I want an << operator for each of them. class Value{}; // this would...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
1
by: Chris | last post by:
This may be more of a Visual Studio question but those groups seem to be full of unrelated stuff so hopefully this might be the right place. I have a class (no associated aspx file) which handles...
10
by: arnuld | last post by:
this is from Steve Summit C notes: The empty pair of parentheses indicates that our main function accepts no arguments, that is, there isn't any information which needs to be passed in when the...
2
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
0
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner;...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.