specialized constructor arg signitaure for const object

From: Jeff Hill (jeffreyhill_at_att.net)
Date: 06/28/04


Date: Mon, 28 Jun 2004 11:46:10 -0600

Hello,

I find that when I am designing a handle class that has an internal
reference to another class that the handle class neither creates or
destroys then I must always provide two versions: a const and a
non-const version. It seems that I could easily avoid this complication
if the language provided a mechanism for specifying a constructor that
is enforced at compile time to be used only when constructing a const
object. The constructor argument signature could be different depening
on whether a const object is being constructed or not. This would allow
the class designer to require that a const reference be passed in when a
const object is being constructed.

One possible option would be to make this distinction by adding const
to the constructor declaration. This would imply that the constructor
argument signature could be used only to construct const objects, and
would *not* imply that the "this" pointer in the constructor was const.

class b;

class a {
    a ( b & );
    a ( const b & ) const;
}

Thanks for listening.

Jeff



Relevant Pages

  • Re: Help with error
    ... > * a copy constructor ... > Test(const Test& Arg) ... > struct Coord ...
    (alt.comp.lang.learn.c-cpp)
  • Re: initonly array member as an lvalue
    ... "...assignments to the fields introduced by the declaration can only occur ... "The readonly keyword is different from the const keyword. ... can be initialized either at the declaration or in a constructor. ... It doesn't read like it should just apply to the reference. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Is this legal?
    ... | Well effectively, inside a class, the meaning of 'const' is the ... a class without a constructor, and just as importantly, ... instantiated, but by the same token, if an initialiser ... Thanks Bart. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Pointer to a class with dynamic memory
    ... _A is reserverd for the implementation. ... > Matrix(unsigned const rows,unsigned const ... You could create another Matrix object outside the block and copy fM ... your class is missing a copy constructor. ...
    (comp.lang.cpp)
  • Re: std::string <--> System::String*
    ... return szAnsi; ... operator const char*() const ... constructor, assignment operator} then you probably need to provide all ...
    (microsoft.public.dotnet.languages.vc)