Re: accessing base class member data .. more

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 12/11/03


Date: Thu, 11 Dec 2003 09:23:52 +0100

ma740988 wrote:
>
> # include <iostream>
>
> class Base
> {
>
> protected:
> int a_, b_;
> static const int c_ = 10; // doesnt work under VC 6.0. Works under
> Comeau

To make it work under VC 6.0 change that

    static const int c_;

But this only declares the variable (introduces it to the compiler).
To actually define it, you put outside the class:

const int Base::c_ = 10;

> _--------------------------------------
>
> Now bear with me on a stuct definition question, which is also
> something i saw in the same source file.
>
> Consider the struct .
> // located in header.h
> struct MM
> {
> int a;
> int b;
> int c;
> };
>
> // in foo.h
> class FOO
> {
> private:
> MM aaa;
> // many more
>
> };
>
> // foo.cpp
> FOO::FOO()
> {
> //
> }
>
> FOO::Initialize()
> {
> aaa.a = 10;
> aaa.b = 5;
> aaa.c = 6;
> }
>
> I realize the approach above is 'perhaps' neater and has a 'named
> notation' (appropriate term in C?? - well at least in Ada) ring to it
> but what would be wrong with doing
>
> FOO::FOO()
> {
> MM aaa = { 10, 5, 6 }; // yeah i loose 'named notation'
> }
> AND inside the constructor?

That does a differen thing.
It creates a local variable named 'aaa' (Yes, it can have the same
name as the member variable), and intializes it. After the ctor
has finished, that local variable is destroyed and the member variable
aaa was left untouched.

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: accessing base class member data .. more
    ... > But this only declares the variable. ... existent after the call to the constructor. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Type alias in C#
    ... Not all value types are structures, for example, enum. ... Lutz Roeder's refector declares Enum as a 'public ... here is that the term "struct" and "value type" are being passed around as synonomous -- which they really aren't. ... An enum is a class that derives from System.Enum. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: is this code valid ?
    ... It does seem like this should compile as well. ... What version of Comeau are you using, CBuilderX ... > struct MPT_ContainsFuncMember ...
    (comp.lang.cpp)
  • Re: memset doubt
    ... > calling memset function for a structure? ... Assuming that you've remembered to #include, which declares ... Given a declaration of "struct some_structure", ... also declared it as a typedef. ...
    (comp.lang.c)
  • Re: mutually referential (Pg 140 K&R2)
    ... >> struct s *p declares p using an incomplete struct s type, ... You can't forward declare an enum ...
    (comp.lang.c)