473,388 Members | 1,375 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,388 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 2279
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.