Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constness and inheritance

Reply
Thread Tools

constness and inheritance

 
 
sndive@gmail.com
Guest
Posts: n/a
 
      07-26-2007
struct A {
virtual int foo(const int bar);
};

int A::foo(const int bar)
{
return 0;
}
struct B : public A {
int foo(int bar);
};
int B::foo(int bar)
{
return 1;
}

int
main()
{
A *p = new B;
const int baz=-1;
int r = p->foo(baz);
return 0;
}

here B::foo is called (tried with g++3.2.3)
i wonder if it's a problem for non elementary types in parameters
since derived function can modify the baz argument with impunity
contrary to the expectations of the caller.
Granted, B::foo could've taken bar as the const parameter
and casted to non const but there are no casts of any
kind in the program above.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> struct A {
> virtual int foo(const int bar);
> };
>
> int A::foo(const int bar)
> {
> return 0;
> }
> struct B : public A {
> int foo(int bar);


This 'foo' *overrides* 'A::foo' because it has exactly same type.

> };
> int B::foo(int bar)
> {
> return 1;
> }
>
> int
> main()
> {
> A *p = new B;
> const int baz=-1;
> int r = p->foo(baz);
> return 0;
> }
>
> here B::foo is called (tried with g++3.2.3)


As it bloody well should.

> i wonder if it's a problem for non elementary types in parameters
> since derived function can modify the baz argument with impunity
> contrary to the expectations of the caller.


Top-level const qualifiers are ignored in the function declaration
as far as type matching is concerned. In your program 'B::foo'
and 'A::foo' have *exactly same* type.

> Granted, B::foo could've taken bar as the const parameter
> and casted to non const but there are no casts of any
> kind in the program above.


The top-level 'const' doesn't matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-27-2007
On Jul 26, 8:05 pm, (E-Mail Removed) wrote:
> struct A {
> virtual int foo(const int bar);
> };


> int A::foo(const int bar)
> {
> return 0;}


> struct B : public A {
> int foo(int bar);};


> int B::foo(int bar)
> {
> return 1;
> }


> int
> main()
> {
> A *p = new B;
> const int baz=-1;
> int r = p->foo(baz);
> return 0;
> }


> here B::foo is called (tried with g++3.2.3)


Correct. Top level const in a function argument is ignored in
function types.

> i wonder if it's a problem for non elementary types in parameters
> since derived function can modify the baz argument with impunity
> contrary to the expectations of the caller.


What expectations of the caller? All the caller can expect is
never to see the parameter. It's call by copy, remember.

--
James Kanze (GABI Software) email:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference counting and constness Marcel Müller C++ 3 12-28-2012 04:28 PM
closure type for lambda-expressions and constness Paul Bibbings C++ 2 04-22-2010 11:53 AM
Constness of the container of shared_ptr and the constness of it elements PengYu.UT@gmail.com C++ 14 04-04-2006 01:57 AM
reference to reference and constness problems with bind1st Marc C++ 6 07-06-2004 02:01 PM
Casting away constness Martin Magnusson C++ 1 11-17-2003 01:30 PM



Advertisments