Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [Newbie] Namespace and inheritance...

Reply
Thread Tools

[Newbie] Namespace and inheritance...

 
 
plug.gulp@gmail.com
Guest
Posts: n/a
 
      07-13-2012
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
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-13-2012
On 7/13/2012 4:54 PM, 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
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      07-14-2012
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
 
Bo Persson
Guest
Posts: n/a
 
      07-14-2012
Bo Persson skrev 2012-07-14 13:51:
> 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
 
Jamie
Guest
Posts: n/a
 
      07-15-2012
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.

<> wrote in message
news:b03e953c-31f1-4fc2-bb77-...
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

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
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Namespace: Is it a scope or a namespace? Anonymous C++ 3 08-18-2003 01:31 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. Èý¹â ASP .Net 1 07-29-2003 04:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57