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! 4 3390
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;
};
<jo*********@gm ail.com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.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
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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 process
leaves resultant data structured in files, in python syntax, at a
known absolute path, which is then read in, eval()'ed, and processed...
|
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 "Some code here"
class class2:
propA = "A"
def methB(self):
|
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 effectively impossible to forward declare
std::string. (Yes I am aware that some libraries have a string_fwd.h header,
but this is not portable.)
That...
|
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 be "public interface Value{}" in Java!
class IntValue : public Value{
private:
int _value;
public:
|
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 templates:
------------------------------------------
in file "Model.h":
| |
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 all the common
startup code for the pages on our website. It is derived from
System.Web.UI.Page and every page in 2 separate projects (web...
|
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 function is called.
i think, an "empty pair" of parenthesis means a function can take
unlimited number of arguments
|
by: talkaboutquality |
last post by:
Need to define a macro as, say,
#ifdef NEED_FUNCTION
foo(x)
#else
#endif
|
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;
public class project
{
public static void main( String args )
{
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |