473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Class members that MUST be initialized by temporary objects in amember initialization list

Hello C++ gurus,

I've got this basic class thats the backbone of my project and I
frequently use use an instance of it to initialize other objects like
so. However, my latest class, a simple assignment in a constructor did
not work, like so:

class VeryBasicClass{
public:
VeryBasicClass(){};
};

class LatestClass{
public:
LatestClass( VeryBasicClass &vbo );
private:
VeryBasicClass veryBasicObject;
};

LatestClass::LatestClass( VeryBasicClass &vbo )
{
veryBasicObject = vbo; // this does not work
// halts plugin execution
}

The following constructor caused the environment (I'm developing a
plugin) to crash with a BLOCK_TYPE_IS_VALID assert

LatestClass::LatestClass( VeryBasicClass &vbo ):veryBasicObject( vbo )
{
}

The following constructor however worked

LatestClass::LatestClass( VeryBasicClass
&vbo ):veryBasicObject( VeryBasicObject() )
{
}

I would know why the first why the first two constructors failed, and
the only the third works, and I'd hate to be in the dark about these
things. Its better to know whats going on in case something goes
wrong.

All thats unique about VeryBasicClass is that itself contains a member
object that must be initialized by a temporary object in a member
initialization lists of a constructor. I'm not the author of the
members class (its part of the plugin API).

Thanks

- Olumide
Dec 6 '07 #1
2 1701
Olumide <50***@web.dewrote in news:30b3ed9c-fdfb-4d2a-b38f-
11**********@t47g2000hsc.googlegroups.com:
Hello C++ gurus,

I've got this basic class thats the backbone of my project and I
frequently use use an instance of it to initialize other objects like
so. However, my latest class, a simple assignment in a constructor did
not work, like so:

class VeryBasicClass{
public:
VeryBasicClass(){};
};

class LatestClass{
public:
LatestClass( VeryBasicClass &vbo );
private:
VeryBasicClass veryBasicObject;
};

LatestClass::LatestClass( VeryBasicClass &vbo )
{
veryBasicObject = vbo; // this does not work
// halts plugin execution
}

The following constructor caused the environment (I'm developing a
plugin) to crash with a BLOCK_TYPE_IS_VALID assert

LatestClass::LatestClass( VeryBasicClass &vbo ):veryBasicObject( vbo )
{
}

The following constructor however worked

LatestClass::LatestClass( VeryBasicClass
&vbo ):veryBasicObject( VeryBasicObject() )
{
}

I would know why the first why the first two constructors failed, and
the only the third works, and I'd hate to be in the dark about these
things. Its better to know whats going on in case something goes
wrong.

All thats unique about VeryBasicClass is that itself contains a member
object that must be initialized by a temporary object in a member
initialization lists of a constructor. I'm not the author of the
members class (its part of the plugin API).
To paraphrase your answer: "The only interesting part of VeryBasicClass
is the part I'm not going to show you".

Provide a _minimal_, _compilable_ example which shows your problem.
Dec 6 '07 #2
On Dec 6, 3:53 am, "Alf P. Steinbach" <al...@start.nowrote:
* Olumide:
I've got this basic class thats the backbone of my project and I
frequently use use an instance of it to initialize other objects like
so. However, my latest class, a simple assignment in a constructor did
not work, like so:
class VeryBasicClass{
public:
VeryBasicClass(){};
};
class LatestClass{
public:
LatestClass( VeryBasicClass &vbo );
private:
VeryBasicClass veryBasicObject;
};
LatestClass::LatestClass( VeryBasicClass &vbo )
{
veryBasicObject = vbo; // this does not work
// halts plugin execution
}
The following constructor caused the environment (I'm
developing a plugin) to crash with a BLOCK_TYPE_IS_VALID
assert
Hmmm. It doesn't cause any problems in my environment. Are you
sure you're showing us the complete code, and that
VeryBasicClass is really that simple.
LatestClass::LatestClass( VeryBasicClass &vbo ):veryBasicObject( vbo )
{
}
The following constructor however worked
LatestClass::LatestClass( VeryBasicClass
&vbo ):veryBasicObject( VeryBasicObject() )
{
}
I would know why the first why the first two constructors failed, and
the only the third works, and I'd hate to be in the dark about these
things. Its better to know whats going on in case something goes
wrong.
All thats unique about VeryBasicClass is that itself
contains a member object that must be initialized by a
temporary object in a member initialization lists of a
constructor. I'm not the author of the members class (its
part of the plugin API).
It seems that at the end of writing the above, in the very
last paragraph, a glimmer of insight started to surface. Try
to follow that thought. All that came before is worthless for
diagnosing what the technical problem is.
I'm not that sure. What's the difference between the last two?
About the only thing I can see is that there is something wrong
in the copy constructor of VeryBasicClass, and that the compiler
optimizes the actual call of the copy constructor out in the
last case.

The fact remains that the code he has posted is fine. But since
the code he has posted isn't the code which is causing him
problems, that doesn't help us (or him) much.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 9 '07 #3

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

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
11
by: Neil Zanella | last post by:
Hello, When an "std::map<int, int> foobar;" statement appears in a C++ program followed by a statement of the form "foobar;" where nothing has ever been inserted at position 123 into map foobar,...
5
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
17
by: Mark | last post by:
uhhmmm... not really sure how to word this. i cant get get this to compile.. i'm not sure what the proper syntax to do this is.. hopefully it's self explanatory. here's my class: ------ class...
19
by: Chocawok | last post by:
Some of the classes in my app are graphical. To encapsulate the graphical side of things I had created a class called "sprite" which holds a bit map and knows how to draw itself etc. The...
14
by: Glen Dayton | last post by:
While looking at some old code I ran across a snippet that increments a pointer to access different members of a structure: .... struct Badness { std::string m_begin; std::string m_param1;...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.