473,791 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a pointer of constructor

While going through my company's existing codebase, I saw a bunch of
weird lines.

Take a look at this one:

class A
{
public:
A(Foo *f) : _f(f) {}

private:
Foo *_f;
};

class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};

How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn't the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?

What does this mean?

May 3 '07 #1
32 2237
Interesting...A re you sure this is compilable code?
g++ returns the following error :
error: invalid cast of an rvalue expression of type 'Foo' to type
'Foo&'
On May 3, 1:14 pm, seank76 <sean...@gmail. comwrote:
While going through my company's existing codebase, I saw a bunch of
weird lines.

Take a look at this one:

class A
{
public:
A(Foo *f) : _f(f) {}

private:
Foo *_f;

};

class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}

};

How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn't the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?

What does this mean?

May 3 '07 #2
On May 3, 2:16 pm, pmouse <pmo...@cogeco. cawrote:
Interesting...A re you sure this is compilable code?
g++ returns the following error :
error: invalid cast of an rvalue expression of type 'Foo' to type
'Foo&'

On May 3, 1:14 pm, seank76 <sean...@gmail. comwrote:
While going through my company's existing codebase, I saw a bunch of
weird lines.
Take a look at this one:
class A
{
public:
A(Foo *f) : _f(f) {}
private:
Foo *_f;
};
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};
How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn't the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?
What does this mean?
I can definitely compile this using Sun forte 6.2 compiler.
Did you make sure to define class Foo correctly?
May 3 '07 #3
seank76 wrote:
While going through my company's existing codebase, I saw a bunch of
weird lines.

Take a look at this one:

class A
{
public:
A(Foo *f) : _f(f) {}

private:
Foo *_f;
};

class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};

How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn't the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?

What does this mean?
It means you have problems.

The first thing that comes to mind is that &(Foo &)Foo()) might return
the address of a temporary. This means that A(Foo *f) : _f(f) {} is
being initialized to somthing that will be destroyed shortly after the
constructor for A returns.

All around, a very bad thing to be doing.
May 3 '07 #4
On May 3, 2:51 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
seank76 wrote:
While going through my company's existing codebase, I saw a bunch of
weird lines.
Take a look at this one:
class A
{
public:
A(Foo *f) : _f(f) {}
private:
Foo *_f;
};
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};
How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn't the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?
What does this mean?

It means you have problems.

The first thing that comes to mind is that &(Foo &)Foo()) might return
the address of a temporary. This means that A(Foo *f) : _f(f) {} is
being initialized to somthing that will be destroyed shortly after the
constructor for A returns.

All around, a very bad thing to be doing.
That was my first response when I first saw this code.
However, a colleague of mine has claimed although it is "temporary" ,
the scope of it belongs to the class instance, NOT the constructor,
hence it is ok.
I don't know if this is right. (The code has been in production for
years so he must be right though...)
I will try to create a test program to validate.

In any case, I just wanted to know if this is a common programming
tactic by anybody.

May 3 '07 #5
seank76 wrote:
....
>
That was my first response when I first saw this code.
However, a colleague of mine has claimed although it is "temporary" ,
the scope of it belongs to the class instance, NOT the constructor,
hence it is ok.
That would be one weird compiler.

In standard C++ it is not, it's probably not even standard C++.
I don't know if this is right. (The code has been in production for
years so he must be right though...)
or lucky.
I will try to create a test program to validate.

In any case, I just wanted to know if this is a common programming
tactic by anybody.
I have never used it.
May 3 '07 #6
* seank76:
On May 3, 2:51 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
>seank76 wrote:
>>While going through my company's existing codebase, I saw a bunch of
weird lines.
Take a look at this one:
class A
{
public:
A(Foo *f) : _f(f) {}
private:
Foo *_f;
};
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};

That was my first response when I first saw this code.
However, a colleague of mine has claimed although it is "temporary" ,
the scope of it belongs to the class instance, NOT the constructor,
hence it is ok.
Bullshit.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 3 '07 #7
On May 3, 4:09 pm, "Alf P. Steinbach" <a...@start.now rote:
* seank76:
On May 3, 2:51 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
seank76 wrote:
While going through my company's existing codebase, I saw a bunch of
weird lines.
Take a look at this one:
class A
{
public:
A(Foo *f) : _f(f) {}
private:
Foo *_f;
};
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};
That was my first response when I first saw this code.
However, a colleague of mine has claimed although it is "temporary" ,
the scope of it belongs to the class instance, NOT the constructor,
hence it is ok.

Bullshit.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Just compiled and ran a test program without any errors using Sun
forte and gcc1.2

the test program is the following:

#include <iostream>
#include <fstream>

using namespace std;

class A
{
public:
A() {}

void doThat() { cout << "HELLOSE SEAN!" << endl; }
};

class B
{
protected:
A* _a;

public:
B(A *a) : _a(a) {}

};

class C : B
{
public:
C() : B((A *)&(const A &)A()) {}

void dodo() { _a->doThat(); }
};

int main()
{
cout << "Starting test." << endl;

C c;

cout << "About the run C.dodo() " << endl;

c.dodo();
return 0;
}

May 3 '07 #8
seank76 wrote:
[..]
Just compiled and ran a test program without any errors using Sun
forte and gcc1.2

the test program is the following:

[..code involving undefined behaviour redacted..]
Go buy a lottery ticket, today is your lucky day.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 3 '07 #9
On May 3, 4:28 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
seank76 wrote:
[..]
Just compiled and ran a test program without any errors using Sun
forte and gcc1.2
the test program is the following:
[..code involving undefined behaviour redacted..]

Go buy a lottery ticket, today is your lucky day.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Wow.

I didn't know there were so many highschool kids in this forum.
Real professionals know they see all kinds of weird **it in the
previous programmers' codes all the time.

But then... I guess this is not really the place to share weird
behaviors of a c++ compiler.

May 3 '07 #10

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

Similar topics

3
4174
by: Sören | last post by:
Hi, I'd like advise on passing ownership of an iostream. The idea is that my factory class/function should open a file, read enough to detect file type (eg which soundfile format), then create a Reader object of the appropriate type for the file. Of course I can call close(), then pass the filename to the reader constructor and reopen the file there.
1
2594
by: J Solowiej | last post by:
Hi, I am wondering: is it possible to pass pointer to member function (non static) to initialize another class? For exmaple: class X { public: typedef double (*F)(double); X(F f) : f_(f) {} private:
58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
3
3282
by: John C | last post by:
Hi, I am a little uncertain about the concept of passing a reference to a class to another instance of a class. for instance I thought that the following was ok: Network network = Network(); Population pool = Population(& network); .... but this doesnt seem to work.. however the following works... Network * network = new Network();
11
1575
by: dave | last post by:
void CalcPortGrossRet(Funds tf,int fsize,PortFolio tp,int cmonth) { int i=0; float totgrossdlrval=0; char converter; while(i<fsize){ tf.enddlrval=(tf.dlrval*(tf.ret/100.0)+tf.dlrval); totgrossdlrval+=tf.enddlrval; i++; }
9
3345
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
8
2101
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
7
5785
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
1
1378
by: stinger5900 | last post by:
First of all I am new to C++. I have created a class, lets say class A. When the constructor is called I create two new classes Class B and Class C, then are not the same class as A. I need to get data from class B into Class C. How should I go about this? I though I could make a function in class A that returns the data, but how do I get a pointer of Class A into Class B so I can access the data from class A Since Class B is created...
18
3274
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
9669
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
9515
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
10426
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...
1
10154
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
9993
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
9029
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
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
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
2913
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.