473,799 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2297
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
6535
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
15
2783
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 as member variables. So including a forward declaration doesnt help, does it? Faced with these, I had the following options: -Include the appropriate header in the header file that contains the class definition that has a member variable that is...
134
7919
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 means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
2
7850
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 the base class that modify the member variable 4. I would like to have the inherited classes override the member variable with a new var that's a subclass of the parent's member variable
2
2697
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 a whole, but the contents a.k.a members, are the declarations (non-inline functions). What I'm trying to understand, is, is storage allocated or not allocated for them at this phase? Next, the members implemented in a .cpp file, for the class,...
1
3305
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 dog's members can access all the cat's private members. // cat.h //
6
8620
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 myfile.h template<typename T,typename R> class some_class;
15
7873
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
8338
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
0
10482
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.