473,473 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

use of member functions in constructors

Here is my queue class. Is it okay to call a private member function
of the class from the constructor?

// custom constructor
MyQueue::MyQueue(unsigned long lInitialSize,
unsigned long lInitialSizeAlignment)
{
this->pbQueue = NULL;
this->InitializeClass(lInitialSize, lInitialSizeAlignment);
}
// default constructor
MyQueue::MyQueue()
{
this->pbQueue = NULL;
this->InitializeClass(DEFAULT_SIZE, DEFAULT_SIZE_ALIGNMENT);
}

// handles construction task
void
MyQueue::InitializeClass(unsigned long lInitialSize,
unsigned long lInitialSizeAlignment)
{
if(pbQueue != NULL)
{
return;
}
if(lInitialSize == 0)
{
lInitialSize = DEFAULT_SIZE;
}
if(lInitialSizeAlignment = 0)
{
lInitialSizeAlignment = DEFAULT_SIZE_ALIGNMENT;
}

this->pbQueue = (LPBYTE) HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY,
lInitialSize);
CheckAllocation(this->pbQueue, 1);

this->pbFront = this->pbQueue;
this->lQueueLength = 0;
this->lQueueMax = lInitialSize;
this->lSizeAlignment = lInitialSizeAlignment;
}
Jul 22 '05 #1
10 2776
Is it okay to call a private member function
of the class from the constructor?

Yes. You can call private member functions of a class from:

A) Within member functions of the class (not necessarily of the current
object). This includes from within any constructors and destructor.

B) Friend functions and friend classes' member functions.
-JKop

Jul 22 '05 #2

"Shailesh Humbad" <no*****@nowhere.com> wrote in message
Here is my queue class. Is it okay to call a private member function
of the class from the constructor?


Yes. In fact this is a way to share common functionality between overloaded
constructors. Also note that virtuality is not achieved inside constructors.

Sharad
Jul 22 '05 #3
Sharad Kala wrote:
"Shailesh Humbad" <no*****@nowhere.com> wrote in message

Here is my queue class. Is it okay to call a private member function
of the class from the constructor?

Yes. In fact this is a way to share common functionality between overloaded
constructors. Also note that virtuality is not achieved inside constructors.

Sharad


I just found a bug because I had done the following in my constructor

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));
...
}

It was working fine until I added an STL list to MyObject. Then I got
an access violation in a begin() function call in an unrelated piece
of code. Eventually, I tracked it down and realized all the internal
pointers of the STL list were being reset to zero (memory leak). This
bug just scared me a little to access the object being constructed
from within the constructor, which is why I asked the question. So my
question is about good style as well.
Jul 22 '05 #4

"Shailesh Humbad" <no*****@nowhere.com> wrote in message
news:Eb*******************@fe2.columbus.rr.com...
Sharad Kala wrote:
"Shailesh Humbad" <no*****@nowhere.com> wrote in message

Here is my queue class. Is it okay to call a private member function
of the class from the constructor?

Yes. In fact this is a way to share common functionality between
overloaded
constructors. Also note that virtuality is not achieved inside
constructors.

Sharad


I just found a bug because I had done the following in my constructor

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));
...
}

It was working fine until I added an STL list to MyObject. Then I got an
access violation in a begin() function call in an unrelated piece of code.
Eventually, I tracked it down and realized all the internal pointers of
the STL list were being reset to zero (memory leak). This bug just scared
me a little to access the object being constructed from within the
constructor, which is why I asked the question. So my question is about
good style as well.


Don't you ever ever do that again :) Initialize the members from the
initialization list of the constructor. The construction mechanism can help
a lot, if you know it well.

Catalin
Jul 22 '05 #5
MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));
...
}

S a t a n l i v e s!
If you have:
struct AggregatePOD
{
int k;
char* p_r;
};
and you want to create a "zero-initialized" object of it, then:
template<class T>
class ValueInitialized : virtual public T
{
public:
ValueInitialized() : T() {}
};

int main()
{
ValueInitialized<AggregatePOD> monkey;

//Another method you'll commonly see:

AggregatePOD ape = AggregatePOD();
}
As for having a class which has member objects which you want to be value
initialized:

#include <string>

class NonAggregateNonPOD
{
std::string blah;
int k;
void* address;

public:
NonAggregateNonPOD() : blah(), k(), address() {}
//The above is how it's done
//ie. in an initializer list
};
Never EVER do the likes of what you did, ie. calling ZeroMemory in the
constructor. (BTW, "ZeroMemory" is a macro for "memset" ). You also would
mess up the pointer to the vtable if you had any virtual functions! (Also,
in my example, you'd be messing up all the memory allocation data in the
std::string object).
-JKop
Jul 22 '05 #6

"Shailesh Humbad" <no*****@nowhere.com> wrote in message
news:Eb*******************@fe2.columbus.rr.com...
I just found a bug because I had done the following in my constructor

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));
...


More than bad style. It doesn't do what you're thinking. Since "this" is a
pointer, sizeof(this) is the size of a pointer, not the size of the current
object.

-Howard
Jul 22 '05 #7
Howard wrote:
"Shailesh Humbad" <no*****@nowhere.com> wrote in message
news:Eb*******************@fe2.columbus.rr.com...

I just found a bug because I had done the following in my constructor

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));
...

More than bad style. It doesn't do what you're thinking. Since "this" is a
pointer, sizeof(this) is the size of a pointer, not the size of the current
object.

Even sizeof(*this) would be a bad idea. Nothing says that
writing zeros all over a class is a good way to initialized
it (even if it's POD).
Jul 22 '05 #8
Shailesh Humbad <no*****@nowhere.com> wrote:
"Shailesh Humbad" <no*****@nowhere.com> wrote in message
Here is my queue class. Is it okay to call a private member function
of the class from the constructor?

I just found a bug because I had done the following in my constructor

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));


'this' is a pointer so you are zeroing four bytes (or whatever),
I think you meant: sizeof *this
...
}


This is nothing to do with privateness or constructors, you
would get the same problem if you did it from a different
member function, or from anywhere else, eg:

MyObject m;
ZeroMemory(&m, sizeof m);

because the object may contain information other than just the
variables you've declared (eg. a virtual dispatch table, memory
allocation info, ...)
Jul 22 '05 #9
JKop wrote:

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));
...
}


S a t a n l i v e s!
If you have:
struct AggregatePOD
{
int k;
char* p_r;
};
and you want to create a "zero-initialized" object of it, then:
template<class T>
class ValueInitialized : virtual public T
{
public:
ValueInitialized() : T() {}
};

int main()
{
ValueInitialized<AggregatePOD> monkey;

//Another method you'll commonly see:

AggregatePOD ape = AggregatePOD();
}
As for having a class which has member objects which you want to be value
initialized:

#include <string>

class NonAggregateNonPOD
{
std::string blah;
int k;
void* address;

public:
NonAggregateNonPOD() : blah(), k(), address() {}
//The above is how it's done
//ie. in an initializer list
};
Never EVER do the likes of what you did, ie. calling ZeroMemory in the
constructor. (BTW, "ZeroMemory" is a macro for "memset" ). You also would
mess up the pointer to the vtable if you had any virtual functions! (Also,
in my example, you'd be messing up all the memory allocation data in the
std::string object).
-JKop

Yeah, I didn't have sizeof(this), I had:

ZeroMemory(this, sizeof(class MyObject));

It did what I wanted it to do since my object only had ints, int
arrays, pointers, and some non-virtual functions. I just wanted to
save time typing out the 25 or so members. My code was mostly C until
recently, so that was just a vestige of the assumptions I was
operating under at that time.

I just started with templates, and I don't really understand your code
above very much. I don't know what POD is. It's okay though, since
my project doesn't have much use for templated objects, inheritance,
or template initializers at this moment.

Jul 22 '05 #10
Shailesh Humbad posted:
JKop wrote:

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this)); ... }
S a t a n l i v e s!
If you have:
struct AggregatePOD {
int k;
char* p_r;
};
and you want to create a "zero-initialized" object of it, then:
template<class T>
class ValueInitialized : virtual public T { public:
ValueInitialized() : T() {} };

int main()
{
ValueInitialized<AggregatePOD> monkey;

//Another method you'll commonly see:

AggregatePOD ape = AggregatePOD(); }
As for having a class which has member objects which you want to be
value initialized:

#include <string>

class NonAggregateNonPOD {
std::string blah;
int k;
void* address;

public:
NonAggregateNonPOD() : blah(), k(), address() {}
//The above is how it's done
//ie. in an initializer list };
Never EVER do the likes of what you did, ie. calling ZeroMemory in the
constructor. (BTW, "ZeroMemory" is a macro for "memset" ). You also
would mess up the pointer to the vtable if you had any virtual
functions! (Also, in my example, you'd be messing up all the memory
allocation data in the std::string object).
-JKop


I just wanted to save time typing out the 25 or so members.

Nor does anyone else. That's why we do:
MyObject blah = {};

MyObject blah = MyObject();

ValueInitialized<MyObject> blah;
-JKop

Jul 22 '05 #11

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

Similar topics

8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
6
by: BigMan | last post by:
Is it safe to call nonvirtual member functions from ctors and dtors? What about virtual ones?
3
by: lovecreatesbeauty | last post by:
Predefined class member functions and inheritance How many member functions on earth can be provided (predefined) by standard-compliant compilers? Scott Meyers says that there are 6:...
21
by: jimmy | last post by:
Hi, I would like to initialize a member array of 3 floats with a memcpy (for reasons of efficiency). Later I would like to access the floats via public members (e.g. foo.x, foo.y, foo.z). Is...
4
by: Jeff Mallett | last post by:
VC++.NET gave me Compiler Error C2621, which states, "A union member cannot have a copy constructor." Yikes, that can't be true! As I understand it, *all* class objects have copy constructors,...
15
by: roberts.noah | last post by:
I ran across some code that called memset(this, 0, sizeof(*this)) in the member function of a structure in C++. This just looked wrong to me so I did some web searching and it does indeed seem...
3
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
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,...
0
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...
0
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,...
0
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...
1
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...
0
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,...
1
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...
0
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...
0
muto222
php
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.