473,793 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nonstatic member example?

fl
Hi,
There is a question about nonstatic member. C++ primer says: A
nonstatic member is restricted to being declared as a pointer or
reference to an object of its class. It only gives an example of
pointer *b.

class Bar {
public:

private:
static Bar a; // OK
Bar *b; // OK
Bar c; // error

My question is how a nonstatic member is declared as a reference to an
object of its class. Because a reference is legal only after the
original variable has been declared, where is the original object? I
feel it is really bizarre. Could you give me an example? Thanks in
advance.


private variable
Dec 31 '07 #1
6 2215
fl wrote:
Hi,
There is a question about nonstatic member. C++ primer says: A
nonstatic member is restricted to being declared as a pointer or
reference to an object of its class. It only gives an example of
pointer *b.

class Bar {
public:

private:
static Bar a; // OK
Bar *b; // OK
Bar c; // error

My question is how a nonstatic member is declared as a reference to an
object of its class. Because a reference is legal only after the
original variable has been declared, where is the original object? I
feel it is really bizarre. Could you give me an example? Thanks in
advance.
Passing it as a parameter to the constructor is one way. this (the instance
pointer) is another. In fact, class member references have to be
initialized in the constructor initialization list (I know of no other way)
and passing as a paramter would be the usuall way. Something like (untested
code)

class Bar {
public:
Bar( Bar& foo ): d( foo ) {}
private:
Bar& d;
};
--
Jim Langston
ta*******@rocke tmail.com
Dec 31 '07 #2
On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:
Hi,
There is a question about nonstatic member. C++ primer says: A
nonstatic member is restricted to being declared as a pointer or
reference to an object of its class. It only gives an example of
pointer *b.

class Bar {
public:

private:
static Bar a; // OK
Bar *b; // OK
Bar c; // error

My question is how a nonstatic member is declared as a reference to an
object of its class. Because a reference is legal only after the
original variable has been declared, where is the original object? I
feel it is really bizarre. Could you give me an example? Thanks in
advance.

private variable
the original object, in a special case like this one, would have to
refer to itself, which would then basicly mean that such a class could
not have a static member since the static member has no 'this'.
Its a special case, don't dissmiss references. They solve many, many
problems.

[10.7] Should you use the this pointer in the constructor?
http://www.parashift.com/c++-faq-lit....html#faq-10.7

#include <iostream>

class A
{
const A& r_a;
public:
A() : r_a(*this) { }
A(const A& copy) : r_a(copy) { }
A& operator=(const A& rhv); // disabled
A const& get_r() const { return r_a; }
};

void foo(const A& r)
{
std::cout << "&r = " << &r;
std::cout << "\tr.r_a = " << &r.get_r();
std::cout << std::endl;
}

int main()
{
A a;
foo(a);
A another = a; // is NOT an assignment
foo(another);
}

/*
&r = 0x7fff0f2e1930 r.r_a = 0x7fff0f2e1930
&r = 0x7fff0f2e1920 r.r_a = 0x7fff0f2e1930
*/

Dec 31 '07 #3
fl
On 31 déc, 04:27, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:


Hi,
There is a question about nonstatic member. C++ primer says: A
nonstatic member is restricted to being declared as a pointer or
reference to an object of its class. It only gives an example of
pointer *b.
class Bar {
public:
private:
static Bar a; * * *// OK
Bar *b; * * * * * * *// OK
Bar c; * * * * * * * // error
My question is how a nonstatic member is declared as a reference to an
object of its class. Because a reference is legal only after the
original variable has been declared, where is the original object? I
feel it is really bizarre. Could you give me an example? Thanks in
advance.
private variable

the original object, in a special case like this one, would have to
refer to itself, which would then basicly mean that such a class could
not have a static member since the static member has no 'this'.
Its a special case, don't dissmiss references. They solve many, many
problems.

[10.7] Should you use the this pointer in the constructor?http://www.parashift.com/c++-faq-lit....html#faq-10.7

#include <iostream>

class A
{
* const A& r_a;
public:
* A() : r_a(*this) { }
* A(const A& copy) : r_a(copy) { }
* A& operator=(const A& rhv); // disabled
* A const& get_r() const { return r_a; }

};

void foo(const A& r)
{
* std::cout << "&r = " << &r;
* std::cout << "\tr.r_a = " << &r.get_r();
* std::cout << std::endl;

}

int main()
{
* A a;
* foo(a);
* A another = a; // is NOT an assignment
* foo(another);

}

/*
&r = 0x7fff0f2e1930 * * r.r_a = 0x7fff0f2e1930
&r = 0x7fff0f2e1920 * * r.r_a = 0x7fff0f2e1930
*/- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
Hi,
I find the modified code,see below, has the same output as yours.
Why the overload:
A another = a; // is NOT an assignment
does not take effect? Thank you very much.

---------------------
#include <iostream>
class A
{
const A& r_a;
public:
A() : r_a(*this) { }
// A(const A& copy) : r_a(copy) { }
// A& operator=(const A& rhv); // disabled
A const& get_r() const { return r_a; }

};
void foo(const A& r)
{
std::cout << "&r = " << &r;
std::cout << "\tr.r_a = " << &r.get_r();
std::cout << std::endl;
}
int main()
{
A a;
foo(a);
A another; // = a; // is NOT an assignment
foo(another);
}
Dec 31 '07 #4
fl
On 31 déc, 04:27, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:


Hi,
There is a question about nonstatic member. C++ primer says: A
nonstatic member is restricted to being declared as a pointer or
reference to an object of its class. It only gives an example of
pointer *b.
class Bar {
public:
private:
static Bar a; * * *// OK
Bar *b; * * * * * * *// OK
Bar c; * * * * * * * // error
My question is how a nonstatic member is declared as a reference to an
object of its class. Because a reference is legal only after the
original variable has been declared, where is the original object? I
feel it is really bizarre. Could you give me an example? Thanks in
advance.
private variable

the original object, in a special case like this one, would have to
refer to itself, which would then basicly mean that such a class could
not have a static member since the static member has no 'this'.
Its a special case, don't dissmiss references. They solve many, many
problems.

[10.7] Should you use the this pointer in the constructor?http://www.parashift.com/c++-faq-lit....html#faq-10.7

#include <iostream>

class A
{
* const A& r_a;
public:
* A() : r_a(*this) { }
* A(const A& copy) : r_a(copy) { }
* A& operator=(const A& rhv); // disabled
* A const& get_r() const { return r_a; }

};

void foo(const A& r)
{
* std::cout << "&r = " << &r;
* std::cout << "\tr.r_a = " << &r.get_r();
* std::cout << std::endl;

}

int main()
{
* A a;
* foo(a);
* A another = a; // is NOT an assignment
* foo(another);

}

/*
&r = 0x7fff0f2e1930 * * r.r_a = 0x7fff0f2e1930
&r = 0x7fff0f2e1920 * * r.r_a = 0x7fff0f2e1930
*/- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
Sorry, my previous response is not right, i.e. Your's

A another = a; // is NOT an assignment

is right.
My question now is: why there is no effect when I comment out:
// A(const A& copy) : r_a(copy) { }
// A& operator=(const A& rhv); // disabled

Maybe the system does the copy in a special initializer fashion?
I know now the object of "A" class is an address, which points to
itself. That is from the following two lines. Right?
const A& r_a;
public:
A() : r_a(*this) { }

---------------
Then, what's the meaning of "another" after "another=a" ?
I am not even clear:

A(const A& copy) : r_a(copy) { }
Thanks.
Dec 31 '07 #5
fl <rx*****@gmail. comwrote:
There is a question about nonstatic member. C++ primer says: A
nonstatic member is restricted to being declared as a pointer or
reference to an object of its class. It only gives an example of
pointer *b.

class Bar {
public:

private:
static Bar a; // OK
Bar *b; // OK
Bar c; // error

My question is how a nonstatic member is declared as a reference to an
object of its class. Because a reference is legal only after the
original variable has been declared, where is the original object? I
feel it is really bizarre. Could you give me an example? Thanks in
advance.
I have yet to see why one should have a non-static reference in a class
in any case, no matter what it refers to.
Dec 31 '07 #6
On Dec 31, 10:35 am, fl <rxjw...@gmail. comwrote:
On 31 déc, 04:27, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 30, 11:24 pm, fl <rxjw...@gmail. comwrote:
Hi,
There is a question about nonstatic member. C++ primer says: A
nonstatic member is restricted to being declared as a pointer or
reference to an object of its class. It only gives an example of
pointer *b.
class Bar {
public:
private:
static Bar a; // OK
Bar *b; // OK
Bar c; // error
My question is how a nonstatic member is declared as a reference to an
object of its class. Because a reference is legal only after the
original variable has been declared, where is the original object? I
feel it is really bizarre. Could you give me an example? Thanks in
advance.
private variable
the original object, in a special case like this one, would have to
refer to itself, which would then basicly mean that such a class could
not have a static member since the static member has no 'this'.
Its a special case, don't dissmiss references. They solve many, many
problems.
[10.7] Should you use the this pointer in the constructor?http://www.parashift.com/c++-faq-lit....html#faq-10.7
#include <iostream>
class A
{
const A& r_a;
public:
A() : r_a(*this) { }
A(const A& copy) : r_a(copy) { }
A& operator=(const A& rhv); // disabled
A const& get_r() const { return r_a; }
};
void foo(const A& r)
{
std::cout << "&r = " << &r;
std::cout << "\tr.r_a = " << &r.get_r();
std::cout << std::endl;
}
int main()
{
A a;
foo(a);
A another = a; // is NOT an assignment
foo(another);
}
/*
&r = 0x7fff0f2e1930 r.r_a = 0x7fff0f2e1930
&r = 0x7fff0f2e1920 r.r_a = 0x7fff0f2e1930
*/- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -

Sorry, my previous response is not right, i.e. Your's

A another = a; // is NOT an assignment

is right.
My question now is: why there is no effect when I comment out:
// A(const A& copy) : r_a(copy) { }
// A& operator=(const A& rhv); // disabled
the compiler generates the copy ctor if you don't. it probably does
the exact same as the one commented out.
I prefer declaring it in cases i want to diagnose problems and assert
theories.

A(const A& copy) : r_a(copy) { std::cout << "copy A"; }

is nice to have when observing and troubleshooting .

>
Maybe the system does the copy in a special initializer fashion?
not at all. Its a member-wise copy, nothing special.

Its the same with something like
struct K
{
int n;
double d;
char c;
};

You can initialize an instance of K and you can copy it because the
compiler generates a copy ctor for you. It also generates a defult
ctor and an assignment operator (if needed).
I know now the object of "A" class is an address, which points to
itself. That is from the following two lines. Right?
const A& r_a;
public:
A() : r_a(*this) { }

---------------
Then, what's the meaning of "another" after "another=a" ?
I am not even clear:

A(const A& copy) : r_a(copy) { }

Thanks.
instance 'another' is a psuedo-copy of a. The difference is that
another's member reference doesn't refer to another.
This isn't something you need not worry about, you'll not find such a
strategy in code out there.
You do need to understand ctor, copy ctor and init list.
Dec 31 '07 #7

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

Similar topics

8
2971
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100; void functionName(int parameterName) }; void ClassName::functionName(int parameterName=constVarName)
3
5141
by: Clay_Culver | last post by:
I have this code: typedef BraidedNode* (BraidedNode::*NodeGet)() const; NodeGet test() { return BraidedNode::getNextID; } This code compiles under MSVC 7.1, but g++ (GCC 3.4.4) will not compile
7
2523
by: Chris Clement | last post by:
I have been handed a project that someone else started and most of it was developed in the VS.NET design mode. For whatever reasons, when I try to make changes to the controls in VS.NET design mode, I suddenly get a ton of these errors: cs(1189): 'class.form.checkedListBox1' denotes a 'field' where a 'class' was expected I was not getting any errors until I made a couple of changes within VS.NET. So I'm trying to understand why...
7
15015
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following error/warning: "error: invalid use of nonstatic data member " However, he did NOT mention this error in the book explicitly.It happens always in the constructor when you try to initialize some data members in the constructor and try to accsess other data...
10
1315
by: Muffin | last post by:
I am a little new to C# and an have a hard time understanding why I get a nonstatic error. I create an object in my main form that has member properties by using a control. From another form/dialog I try to access that object to set a property. I get a non static error. I can change my object to static, which works fine. Static objects are acting as I expect. Non static objects are not. I do not understand why this is happening. I have...
10
2530
by: Jeffrey | last post by:
My understanding is that if you write class X { int y; static int z; }; then you've defined (and declared) X and y, but you have only declared (and not defined) z. If you'd like to actually define z, you also need to add
4
23048
by: nielsp | last post by:
Hi! Why does'nt the following work? #include <iostream> class X { public:
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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
10212
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...
0
10000
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
9035
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...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.