PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Class members that MUST be initialized by temporary objects in amember initialization list
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Class members that MUST be initialized by temporary objects in amember initialization list

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2007, 01h59   #1
Olumide
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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


  Réponse avec citation
Vieux 06/12/2007, 02h53   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Class members that MUST be initialized by temporary objects ina member initialization list

* Olumide:
> 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).


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.

Cheers, & hth.,

- Alf

--
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?
  Réponse avec citation
Vieux 06/12/2007, 13h34   #3
Andre Kostur
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Class members that MUST be initialized by temporary objects in a member initialization list

Olumide <50295@web.de> wrote in news:30b3ed9c-fdfb-4d2a-b38f-
1182ea60bce4@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.
  Réponse avec citation
Vieux 07/12/2007, 08h50   #4
yurec
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Class members that MUST be initialized by temporary objects in amember initialization list

On Dec 6, 4:53 am, "Alf P. Steinbach" <al...@start.no> wrote:
> * Olumide:
>
>
>
>
>
> > 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).

>
> 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.
>
> Cheers, & hth.,
>
> - Alf
>
> --
> 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?- Hide quoted text -
>
> - Show quoted text -


I think you know, but to be sure : you'll get slicing while copying
VeryBasicClass ( as it's value in your member list)
Maybe something is missed in copying VeryBasicClass?
  Réponse avec citation
Vieux 09/12/2007, 10h10   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Class members that MUST be initialized by temporary objects in amember initialization list

On Dec 6, 3:53 am, "Alf P. Steinbach" <al...@start.no> wrote:
> * 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 us (or him) much.

--
James Kanze (GABI Software) email:james.kanze@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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 1,37330 seconds with 13 queries