Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2012, 08:54 PM
plug.gulp@gmail.com
Guest
 
Posts: n/a
Default [Newbie] Namespace and inheritance...

Hello,

I am learning C++. I wrote the following C++ code to understand namespace and inheritance. It does not compile(g++ 4.6.3), but when I explicitly specify the scope resolution the program works. Why am I not able to directly call the public method implemented in the base class?


namespace N1 {
class C
{
public:
void F(const std::string& s)
{
std::cout << "N1::C::F(str): " << s.c_str() << std::endl;
}
};
};

namespace N2 {
class C : public N1::C
{
public:
void F(int i)
{
std::cout << "N2::C::F(int): " << i << std::endl;
}
};
};

int main()
{
N2::C c;
c.F(1);

// The following statement does not compile unless
// it is called with full scope resolution as follows:
// c.N1::C::F("one");

c.F("one");

return 0;
}

Thanks and regards,

Plug
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-13-2012, 09:06 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: [Newbie] Namespace and inheritance...

On 7/13/2012 4:54 PM, plug.gulp@gmail.com wrote:
> Hello,
>
> I am learning C++. I wrote the following C++ code to understand
> namespace and inheritance. It does not compile(g++ 4.6.3), but when I
> explicitly specify the scope resolution the program works. Why am I not
> able to directly call the public method implemented in the base class?
>
>
> namespace N1 {
> class C
> {
> public:
> void F(const std::string& s)
> {
> std::cout << "N1::C::F(str): " << s.c_str() << std::endl;
> }
> };
> };
>
> namespace N2 {
> class C : public N1::C
> {
> public:
> void F(int i)
> {
> std::cout << "N2::C::F(int): " << i << std::endl;
> }
> };
> };
>
> int main()
> {
> N2::C c;
> c.F(1);
>
> // The following statement does not compile unless
> // it is called with full scope resolution as follows:
> // c.N1::C::F("one");
>
> c.F("one");
>
> return 0;
> }


This is covered by the FAQ. Please read FAQ before posting. You can
find the FAQ Lite here: http://www.parashift.com/c++-faq-lite/. Hint:
see section 23. And crank up the warning level on your compiler, maybe
you will get a useful diagnostic out of that tool for a change...

V
--
I do not respond to top-posted replies, please don't ask


Reply With Quote
  #3 (permalink)  
Old 07-14-2012, 11:51 AM
Bo Persson
Guest
 
Posts: n/a
Default Re: [Newbie] Namespace and inheritance...

plug.gulp@gmail.com skrev 2012-07-13 22:54:
> Hello,
>
> I am learning C++. I wrote the following C++ code to understand namespace and inheritance. It does not compile(g++ 4.6.3), but when I explicitly specify the scope resolution the program works. Why am I not able to directly call the public method implemented in the base class?
>
>
> namespace N1 {
> class C
> {
> public:
> void F(const std::string& s)
> {
> std::cout << "N1::C::F(str): " << s.c_str() << std::endl;
> }
> };
> };
>
> namespace N2 {
> class C : public N1::C
> {
> public:
> void F(int i)
> {
> std::cout << "N2::C::F(int): " << i << std::endl;
> }
> };
> };
>
> int main()
> {
> N2::C c;
> c.F(1);
>
> // The following statement does not compile unless
> // it is called with full scope resolution as follows:
> // c.N1::C::F("one");
>
> c.F("one");
>
> return 0;
> }
>


It has nothing to do with namespaces.

Declaring something named F in the derived class hides the name F from
any base classes. This is similar to declaring local variables in an
inner scope which hides names from outer scopes.

If you want to use the name anyway, you can either use the full name
(like you did), or add a "using N1::C::F;" to class C2.


Bo Persson



Reply With Quote
  #4 (permalink)  
Old 07-14-2012, 11:55 AM
Bo Persson
Guest
 
Posts: n/a
Default Re: [Newbie] Namespace and inheritance...

Bo Persson skrev 2012-07-14 13:51:
> plug.gulp@gmail.com skrev 2012-07-13 22:54:
>> Hello,
>>
>> I am learning C++. I wrote the following C++ code to understand
>> namespace and inheritance. It does not compile(g++ 4.6.3), but when I
>> explicitly specify the scope resolution the program works. Why am I
>> not able to directly call the public method implemented in the base
>> class?
>>
>>
>> namespace N1 {
>> class C
>> {
>> public:
>> void F(const std::string& s)
>> {
>> std::cout << "N1::C::F(str): " << s.c_str() <<
>> std::endl;
>> }
>> };
>> };
>>
>> namespace N2 {
>> class C : public N1::C
>> {
>> public:
>> void F(int i)
>> {
>> std::cout << "N2::C::F(int): " << i << std::endl;
>> }
>> };
>> };
>>
>> int main()
>> {
>> N2::C c;
>> c.F(1);
>>
>> // The following statement does not compile unless
>> // it is called with full scope resolution as follows:
>> // c.N1::C::F("one");
>>
>> c.F("one");
>>
>> return 0;
>> }
>>

>
> It has nothing to do with namespaces.
>
> Declaring something named F in the derived class hides the name F from
> any base classes. This is similar to declaring local variables in an
> inner scope which hides names from outer scopes.
>
> If you want to use the name anyway, you can either use the full name
> (like you did), or add a "using N1::C::F;" to class C2.


^^^^^^^^
Here "class C2" of course should be "class C in namespace N2".


Bo Persson

Reply With Quote
  #5 (permalink)  
Old 07-15-2012, 12:11 AM
Paul
Guest
 
Posts: n/a
Default Re: [Newbie] Namespace and inheritance...


"Bo Persson" <bop@gmb.dk> wrote in message
news:a6d4qaFkdrU1@mid.individual.net...
> plug.gulp@gmail.com skrev 2012-07-13 22:54:
>> Hello,
>>
>> I am learning C++. I wrote the following C++ code to understand namespace
>> and inheritance. It does not compile(g++ 4.6.3), but when I explicitly
>> specify the scope resolution the program works. Why am I not able to
>> directly call the public method implemented in the base class?
>>
>>
>> namespace N1 {
>> class C
>> {
>> public:
>> void F(const std::string& s)
>> {
>> std::cout << "N1::C::F(str): " << s.c_str() <<
>> std::endl;
>> }
>> };
>> };
>>
>> namespace N2 {
>> class C : public N1::C
>> {
>> public:
>> void F(int i)
>> {
>> std::cout << "N2::C::F(int): " << i << std::endl;
>> }
>> };
>> };
>>
>> int main()
>> {
>> N2::C c;
>> c.F(1);
>>
>> // The following statement does not compile unless
>> // it is called with full scope resolution as follows:
>> // c.N1::C::F("one");
>>
>> c.F("one");
>>
>> return 0;
>> }
>>

>
> It has nothing to do with namespaces.
>
> Declaring something named F in the derived class hides the name F from any
> base classes. This is similar to declaring local variables in an inner
> scope which hides names from outer scopes.
>
> If you want to use the name anyway, you can either use the full name (like
> you did), or add a "using N1::C::F;" to class C2.
>

Will that then hide the F in N2::C ?



--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Reply With Quote
  #6 (permalink)  
Old 07-15-2012, 07:25 AM
Jamie
Guest
 
Posts: n/a
Default Re: [Newbie] Namespace and inheritance...

What is "namespace"? What is a name? Do you know what a "name" is? You use
it like a weapon. So let it be written.

<plug.gulp@gmail.com> wrote in message
news:b03e953c-31f1-4fc2-bb77-0fe79213d04f@googlegroups.com...
Hello,

I am learning C++. I wrote the following C++ code to understand namespace
and inheritance. It does not compile(g++ 4.6.3), but when I explicitly
specify the scope resolution the program works. Why am I not able to
directly call the public method implemented in the base class?


namespace N1 {
class C
{
public:
void F(const std::string& s)
{
std::cout << "N1::C::F(str): " << s.c_str() << std::endl;
}
};
};

namespace N2 {
class C : public N1::C
{
public:
void F(int i)
{
std::cout << "N2::C::F(int): " << i << std::endl;
}
};
};

int main()
{
N2::C c;
c.F(1);

// The following statement does not compile unless
// it is called with full scope resolution as follows:
// c.N1::C::F("one");

c.F("one");

return 0;
}

Thanks and regards,

Plug


Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 04:01 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.