473,804 Members | 3,049 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
32 2238
On May 5, 1:35 pm, pmouse <pmo...@cogeco. cawrote:
On May 4, 10:14 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
pmouse wrote:
On May 4, 11:51 am, seank76 <sean...@gmail. comwrote:
...
~A () { if ( _pF ) delete _pF; }
~A () { delete _pF; }
The if ( _pF ) is already checked by delete.

Not in GCC/g++, and many other compilers that I use.
deleting a piece of memory twice will cause a double free exception
(run time)
On the other hand, free() does check for zero pointers.

Regards,

PQ
nevermind, I wasn't thinking.

Regards,

PQ

May 5 '07 #31
On May 5, 7:35 pm, pmouse <pmo...@cogeco. cawrote:
On May 4, 10:14 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
pmouse wrote:
On May 4, 11:51 am, seank76 <sean...@gmail. comwrote:
...
~A () { if ( _pF ) delete _pF; }
~A () { delete _pF; }
The if ( _pF ) is already checked by delete.
Not in GCC/g++, and many other compilers that I use.
It is in every compiler I've ever used. Including g++.
deleting a piece of memory twice will cause a double free exception
(run time)
But there's nothing in your code above which has anything to do
with deleting an object twice. You're checking if the pointer
is null. (It would, of course, be better to do so explicitly,
rather than relying on implicit conversions.) And invoking
delete on a null pointer is legal, and works perfectly well with
all major compilers.

-
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 5 '07 #32
"seank76" <se*****@gmail. comwrote in message
news:11******** **************@ y5g2000hsa.goog legroups.com...
On May 3, 5:55 pm, "Alf P. Steinbach" <a...@start.now rote:
>* seank76:
[quoting signatures]

Please don't quote signatures, please read the FAQ before posting.

Please don't say Bullshit to a topic you don't understand.
Please read "Internet Etiquette" manual before you post.
Seank, please don't argue with Victor or Alf. They are the de facto C++
gurus in this newsgroup. They know more than everyone else combined. If
they say something is undefined, it is undefined.

Victor never said it was bullshit. He said to go buy a lottery ticket
because today is your lucky day. A tongue in cheek way of saying, you are
extremely lucky the code is working right now.

What your colliege told you, however, is bullshit as far as the C++ standard
is concerned, which is what we discuss in this newsgroup. If it happens to
work on your compiler in it's current version and OS doesn't mean it's not
undefined behavior. Undefined behavior is just that, undefined. Anything
can happen, including working as you want it to work. Just because it works
doesn't mean it's right.

The next version of your compiler the designers may decide to change
something internal that will break your code, as they have all the right to
do, since they only need to support well defined behavior.

Believe me, I have never seen Alf or Victor respond incorrectly to a topic
they didn't understand. It was just that I, or someone else, didn't
understand the topic ourselves. Victor was right, you are [un]lucky it
works. It is a crap shoot. Go ahead and buy yourself a lottery ticket.
May 5 '07 #33

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

Similar topics

3
4176
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
10188
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
3283
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
3346
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
3275
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
10319
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
10303
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
10070
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
9132
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
7608
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
6845
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
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
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
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.