Are member objects constructed before the body of the constructor executes?
Consider the following example. Is this okay or is it possible that _bar
will be created after the call to DoBar()?
class Bar
{
public:
Set(int i) { _i = i; }
private:
int _i;
};
class Foo
{
public:
Foo() { DoBar(); }
private:
void DoBar() { _bar.Set(10); }
Bar _bar;
};
Thanks in advance,
Joost
-----
"People whose concept of ancient history is the first series of Star Trek
may be treated with patience, because it's usually not their fault they were
reduced to getting their education from school." -- Terry Pratchett 9 1364
"Joost Ronkes Agerbeek" <jo***@ronkes.n l> writes: Are member objects constructed before the body of the constructor executes?
Yes. Consider the following example. Is this okay or is it possible that _bar will be created after the call to DoBar()?
No. class Bar { public: Set(int i) { _i = i; }
private: int _i; };
class Foo { public: Foo() { DoBar(); }
private: void DoBar() { _bar.Set(10); } Bar _bar; };
[snip]
"llewelly" <ll*********@xm ission.dot.com> wrote in message
news:86******** ****@Zorthluthi k.local.bar... "Joost Ronkes Agerbeek" <jo***@ronkes.n l> writes:
Are member objects constructed before the body of the constructor executes? Yes.
Consider the following example. Is this okay or is it possible that _bar will be created after the call to DoBar()?
No.
Umm that was an either/or question. :-) I think he means "no: _bar will not
be created after the call to DoBar(), but will be already be created by that
time". In other words, yes, it's ok. class Bar { public: Set(int i) { _i = i; }
private: int _i; };
class Foo { public: Foo() { DoBar(); }
private: void DoBar() { _bar.Set(10); } Bar _bar; };
[snip]
Howard wrote: "llewelly" <ll*********@xm ission.dot.com> wrote in message news:86******** ****@Zorthluthi k.local.bar...
"Joost Ronkes Agerbeek" <jo***@ronkes.n l> writes:
Are member objects constructed before the body of the constructor executes?
Yes.
Consider the following example. Is this okay or is it possible that _bar will be created after the call to DoBar()?
No.
Umm that was an either/or question. :-) I think he means "no: _bar will not be created after the call to DoBar(), but will be already be created by that time". In other words, yes, it's ok.
class Bar { public: Set(int i) { _i = i; }
private: int _i; };
class Foo { public: Foo() { DoBar(); }
private: void DoBar() { _bar.Set(10); } Bar _bar; };
[snip]
The statement is ambiguous: _bar is created as soon as Foo is created and
before the body of Foo() is entered. By the time DoBar() is called, _bar
has already been created (with its member _i uninitialised). So, calling
"DoBar", which in turn calls Bar::Set, is OK.
Victor int _i;
Let me please just take this opportunity to express my disgust at your
coding style.
-JKop
Thanks everyone, for clearing that up.
:-D Any suggestions for how I should clean up my act?
Joost
"JKop" <NU**@NULL.NULL > wrote in message
news:97******** **********@news .indigo.ie... int _i;
Let me please just take this opportunity to express my disgust at your coding style.
-JKop
Joost Ronkes Agerbeek wrote: :-D Any suggestions for how I should clean up my act?
Joost
"JKop" <NU**@NULL.NULL > wrote in message news:97******** **********@news .indigo.ie...
int _i;
Let me please just take this opportunity to express my disgust at your coding style.
-JKop
One solution that may be more to his liking, would be to change your
class as such:
class Bar
{
public:
Set(int i) { Bar::i = i; }
private:
int i;
};
Alan
Joost Ronkes Agerbeek posted: :-D Any suggestions for how I should clean up my act?
Joost
"JKop" <NU**@NULL.NULL > wrote in message news:97******** **********@news .indigo.ie... > int _i;
Let me please just take this opportunity to express my disgust at your coding style.
-JKop
Well, here's what *I* might do: Name the member variable:
m_Chocolate
And name the variable passed to a function:
Chocolate
And for global variables:
g_Chocolate
And for static member variables:
ClassName::s_Ch ocolate
And for a pointer:
pChocolate
-JKop
JKop wrote: Joost Ronkes Agerbeek posted:
:-D Any suggestions for how I should clean up my act?
Joost
"JKop" <NU**@NULL.NULL > wrote in message news:97****** ************@ne ws.indigo.ie...
int _i;
Let me please just take this opportunity to express my disgust at your coding style.
-JKop Well, here's what *I* might do: Name the member variable:
m_Chocolate
And name the variable passed to a function:
Chocolate
And for global variables:
g_Chocolate
And for static member variables:
ClassName::s_Ch ocolate
And for a pointer:
pChocolate
-JKop
Let me please just take this opportunity to express my disgust at your
coding style. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jerivix Entadi |
last post by:
I'm attempting to create an application to work with a fluid database
of people. I'm doing this in a command line, text-based manner. I need
to know two things: 1) How do I save data, perhaps a group of objects
and their memebers or a few lines of text? and 2) Is there any way to
automate object construction? I'm thinking that I can create a vector
or something similar to handle the number of people, is there a way
for me to write a...
|
by: newbiecpp |
last post by:
I have a simple class:
class Point {
public:
Point() : xval(0), yval(0) {}
Point(int x, int y) : xval(x), yval(0) {}
private:
int xval, yval;
};
|
by: trying_to_learn |
last post by:
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling the constructor of the class. However he says when we leave
scope of function HowMany f(HowMany x) then the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not*
be called. i can understand that bit wise copy...
|
by: Dave |
last post by:
Hello all,
In the code below, I use a pointer to an object under construction. Is the
usage below legal? I have come across similar code at work. It compiles,
but I'm not sure it's really legal...
Thanks,
Dave
struct D;
|
by: bluekite2000 |
last post by:
I have a Matrix class derived from an Array class as followed:
Matrix(int nM, int nN)
:Array<T>(nM*nN),mnM(nM),mnN(nN)
{
}
However, I dont want to call Array constructor if nM!=nN. In other
words, I dont want to use the initialization list.
Regards
| |
by: Jakob Bieling |
last post by:
Hi,
I am aware of the fact, that the order of construction of global
objects is unspecified. But I am in a situation, where I need to
guarantee, that one object is created before others (not all others,
just all objects of a specified type). I came up with the following
solution, but I am not sure, if it does solve my problem:
class first
{
|
by: MKoool |
last post by:
Hi everyone,
I am doing several operations on lists and I am wondering if python has
anything built in to get every member of several objects that are in an
array, for example, if i have a class like the following:
class myClass:
a = 0.0
And lets say I populate the "a" element in an array of objects of
|
by: Brian Richards |
last post by:
I have an object that I'm serializing (binary) that contains other internal
objects that are also serialized. The Object contains two other member
objects (let's call them source, and depend) one of which has a state that
depends value is derived from source. My question is (if this can be
generalized) is faster/better to serialize both objects or to only serialize
state and then during deserialization create depend from source.
Option 1:
|
by: Ulrich Hobelmann |
last post by:
Hi, slowly transitioning from C to C++, I decided to remodel a
struct/union (i.e. type identifier as first field, union of variant
types) as a class + subclasses. Switching functions are replaced by
virtual functions. So far so good.
Now what I used to do is have a struct, set its type and union member,
and return a pointer. I.e. I initialized the struct appropriately and
returned a reference. Now I'd like to do that in C++ (right...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |