1
#include <iostream>

class ZZ
{
public:
    void print1()
    {
        std::cout << "hello\n";
    }
};

class YY : public ZZ
{
public:
    void print()
    {
        using ZZ::print1;
        print1();
    }
};

int main()
{
    YY temp;
    temp.print();
    getchar();
    return 0;
}

If I compile the code, I would get the error: error: 'ZZ' is not a namespace or unscoped enum using ZZ::print1;. I'm confused.


If I define another namespace in this file, like this:

namespace tt{
   int a;
}

And I use this in the derived function print, like this:

void print()
{
    using namespace tt;
    a = 1;
}

The code will be compiled successfully.But I think ZZ is a namespace as well because if I use the ZZ like this:

public:
   using ZZ::print1;
   void print()
   {
       print1();
   }

This code will be compiled successfully as well.


So I don't know why this error happened when I use using ZZ::print1 in the derived function print.

mrsiz
  • 193
  • 9
  • Wrong assumption. A class is not a namespace. –  Dec 08 '16 at 06:50
  • I've never encounteed that but that's because the `using` declaration here is nonsense: `ZZ::print1` is directly available because `YY` inherits from `ZZ`. And if `YY` hadn't inherited from `ZZ` it would also be nonsense. But in short, the rules for classes and namespaces, e.g. what `static` and `using` mean, are different. – Cheers and hth. - Alf Dec 08 '16 at 06:50
  • By the way, the `getchar();` to sort of stop the execution at the end, is a foul-things-up-for-you meme. When you get to know your tools, e.g. how to run a program in your IDE, it can only have adverse effect. – Cheers and hth. - Alf Dec 08 '16 at 06:53

3 Answers3

2

Just because you can use ZZ:: syntax does not meat that ZZ is a namespace. The :: is a scope resolution operator that can be used with namespace scopes as well as with class scopes or enumeration scopes. In your case ZZ:: refers to the scope of class ZZ.

Also, the language specification states that

7.3.3 The using declaration

8 A using-declaration for a class member shall be a member-declaration.

which means that a using-declaration that refers to a class member cannot appear in local scope. It can only be used in class scope.

This is why using-declaration in your last code sample compiles successfully.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

C++ standard N3936 states:

The scope resolution operator :: (5.1) can be used to refer to a direct or indirect base member explicitly. This allows access to a name that has been redeclared in the derived class. A derived class can itself serve as a base class subject to access control;

ZZ is not a namespace but a class. Fix it by changing

using ZZ::print1;
print1();

to

ZZ::print1();

Read this How to call a parent class function from derived class function?

Community
  • 1
  • 1
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
-1

As @Cheers already pointed out, you don't need to use "using_declaration" here, as the function will anyhow available to derived class, because of its "public" access specification.

If you still want to use it, then you should move the using outside to function scope. (http://en.cppreference.com/w/cpp/language/using_declaration#In_class_definition)

Bharath
  • 111
  • 9