ansi status of 'using'

M

Mark Huppert

Dear Colleagues

re: 'using' and the ANSI C++ standard

According to the ANSI C++ standard, is this a valid
way (disregarding whether it is 'naughty') of converting a
public method in the base class into a private method in the child class?
My g++ compiler ignores this without comment.

class parent{
public:
double length(double num) {return num; }
};

class child: public parent {
public:
double hiding(double num) {return length(num);}
private:
using parent::length;
};

ie, equivalent to:
private:
double length(double num) {return parent::length(num); }

Mark Huppert
 
I

Ioannis Vranos

Mark Huppert said:
Dear Colleagues

re: 'using' and the ANSI C++ standard

According to the ANSI C++ standard, is this a valid
way (disregarding whether it is 'naughty') of converting a
public method in the base class into a private method in the child class?
My g++ compiler ignores this without comment.

class parent{
public:
double length(double num) {return num; }
};

class child: public parent {
public:
double hiding(double num) {return length(num);}
private:
using parent::length;
};

ie, equivalent to:
private:
double length(double num) {return parent::length(num); }


No, but why doing this? You can do:


class parent{
public:
double length(double num) {return num; }
};

class child: private parent {
public:
double hiding(double num) {return length(num);}
};






Regards,

Ioannis Vranos
 
J

Jeff Schwab

Mark said:
According to the ANSI C++ standard, is this a valid
way (disregarding whether it is 'naughty') of converting a
public method in the base class into a private method in the child class?
No.

My g++ compiler ignores this without comment.

Because it's doing something valid, just not what you think it's doing.
class parent{
public:
double length(double num) {return num; }
};

class child: public parent {
public:
double hiding(double num) {return length(num);}
private:
using parent::length;
};

ie, equivalent to:
private:
double length(double num) {return parent::length(num); }


Are you sure about that? There is no way to make publicly inherited
members "more private." Try this.

class parent{
public:
double length(double num) {return num; }
};

class child: public parent {
public:
double hiding(double num) {return length(num);}
private:
using parent::length;
};

int main ( )
{
child c;

c.length( 3.0 );
}
 
J

Jacek Dziedzic

Mark said:
Dear Colleagues

re: 'using' and the ANSI C++ standard

According to the ANSI C++ standard, is this a valid
way (disregarding whether it is 'naughty') of converting a
public method in the base class into a private method in the child class?
My g++ compiler ignores this without comment.

class parent{
public:
double length(double num) {return num; }
};

class child: public parent {
public:
double hiding(double num) {return length(num);}
private:
using parent::length;
};

ie, equivalent to:
private:
double length(double num) {return parent::length(num); }

The using declaration does "bring the otherwise hidden names
from the base class into scope of the derived class", but I don't
think it does matter whether you put it in the private or protected
or public section. Therefore I'd say it is *not* equivalent.

Hope I got it right this time :)

You could visit
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6
for an example of meaningful usage of 'using' within a class.

HTH,
- J.
 
I

Ivan Vecerina

Mark Huppert said:
Dear Colleagues

re: 'using' and the ANSI C++ standard

According to the ANSI C++ standard, is this a valid
way (disregarding whether it is 'naughty') of converting a
public method in the base class into a private method in the child class?
My g++ compiler ignores this without comment.
[...]

'using' can only *increase* the visibility of an inherited member
(i.e. make a private member public), but not restrict it.
class parent{
public:
double length(double num) {return num; }
};

class child: public parent {
public:
double hiding(double num) {return length(num);}
private:
using parent::length;
};

You cannot do this, and it wouldn't really make sense:
child c;
c.length(); // you want this to be an error?
parent& p = c; // but this is legal and implicit
p.length(); // legal, and no cast needed...

What you can do is privately derive from parent.
Or even better, use *containment* instead
(store child as a data member of parent).


Regards,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,916
Members
47,458
Latest member
Chris#

Latest Threads

Top