473,287 Members | 1,395 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,287 software developers and data experts.

member variable declaration and forward declarations

I'm having issues with forward declarations and possibly member variables.

Can you declare a member variable and pass it parameters.

class x {
private:
y obj(this);
}

Is that valid? I'm getting a lot of problems, but it may not be due to
that. It may be due to the fact that the real X and Y require knowledge
of each other.

Here is a little example illustrating my problem. It would be a better
example if we put foo and bar in different files and included their
headers prior to the class def.

#include <iostream>

namespace myNS {
class bar;

class foo {
private:
bar b(this);

public:
void method() { std::cout << "foo"; }
void do() { b.method(); }
};

class bar {
private:
foo &f;

public:
bar(foo &f) : f(f) {}
void method() { f.method(); }
};
}

int main(int, char **) {
myNS::foo().do();

return 0;
}

This is clearly no good. Is it possible for foo to create a bar instance
b passing itself as a parameter?

Thanks,

--John Ratliff
Aug 26 '05 #1
2 2276
John Ratliff wrote:
I'm having issues with forward declarations and possibly member variables.

Can you declare a member variable and pass it parameters.

class x {
private:
y obj(this);
}

Is that valid?
No. What would that do? Initialise it? Then it wouldn't be
a declaration. Initialisation of data members is done in a constructor
initialiser list.
I'm getting a lot of problems, but it may not be due to
that. It may be due to the fact that the real X and Y require knowledge
of each other.

Here is a little example illustrating my problem. It would be a better
example if we put foo and bar in different files and included their
headers prior to the class def.

#include <iostream>

namespace myNS {
class bar;

class foo {
private:
bar b(this);
That won't fly. If 'bar' is unknown, you can't declare a variable of
that type. You need to move 'bar' class definition above 'foo', and
then change the declaration to read

bar b;

public:
You need to add a constructor declaration/definition here

foo() : b(*this) {}
void method() { std::cout << "foo"; }
void do() { b.method(); }
};

class bar {
private:
foo &f;

public:
bar(foo &f) : f(f) {}
void method() { f.method(); }
};
As I said before, you need to move the 'bar' class definition up, above
the 'foo', and forward-declare 'class foo'.

Then you need to _declare_ 'method' and define it _after_ 'foo' here:

void bar::method() { f.method(); }
}

int main(int, char **) {
myNS::foo().do();

return 0;
}

This is clearly no good. Is it possible for foo to create a bar instance
b passing itself as a parameter?


Yes.

V
Aug 26 '05 #2
Victor Bazarov wrote:
John Ratliff wrote:
I'm having issues with forward declarations and possibly member
variables.

Can you declare a member variable and pass it parameters.

class x {
private:
y obj(this);
}

Is that valid?

No. What would that do? Initialise it? Then it wouldn't be
a declaration. Initialisation of data members is done in a constructor
initialiser list.


I can't believe I forgot about initializer lists...

Thanks,

--John Ratliff
Aug 26 '05 #3

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
2
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in...
2
by: xllx.relient.xllx | last post by:
I have a few quetions about definitions vs declarations concerning defined types (user defined), specifically classes. From what I understand, a class contained in a header file is a definition as...
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.