?
Solved

namespace requirement inside class member function

Posted on 2002-06-21
15
Medium Priority
?
309 Views
Last Modified: 2010-04-01
What is the namespace requirement inside class member function?

namespace WinX_Namespace
{
     class Widget{
     public:
           bool Function(){return false;};
     };
}

class foo : public WinX_Namespace::Widget
{
public:
 bool Function();
};

//In cpp file
bool foo::Function()
{
 return Widget::Function();//Does this need namespace
}


Does the function call to Widget::Function also need the namespace WinX_Namespace.

I can compile the above code to VC++ with no problem, but it will not compile in an Apex C++ compiler.

If I change the code like this
bool foo::Function()
{
 return WinX_Namespace::Widget::Function();
}
Then it will compile in the Apex compiler, but it does not compile in the VC++ compiler.

The VC++ compiler gives me this error
error C2352: 'WinX_Namespace::Widget::Function' : illegal call of non-static member function

So occurring to the C++ standards, which one of these method are right, and which one is wrong?
0
Comment
Question by:Zulma9999
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7099027
Technically speaking, both methods should have been able to compile, so I would say they're both wrong.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7099053
>>error C2352: 'WinX_Namespace::Widget::Function' : illegal call of non-static member function

The compiler is absolutely correct about this:

bool foo::Function()
{
return WinX_Namespace::Widget::Function();
}

bool foo::Function()
{
return Widget::Function();
}

are both calls to a nonstatic member of "Widget". You probably wanted to use

bool foo::Function()
{
WinX_Namespace::Widget w;
return w.Function();
}

or

bool foo::Function()
{
Widget w;
return w.Function();
}

respectively.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7099066
>>re both calls to a nonstatic member of "Widget". You >>probably wanted to use

According to the questioner's code, these are calls to the parrent function.  Remember foo is a decedent of Widget.
So calling Widget::Function(), should call the parrent function.

Moreover, you can accomplish the same thing by calling ::Function(), which should also compile on both compilers.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Zulma9999
ID: 7099073
jkr,
No, I don't want to call a new instance of Widget.  I want to call the base class member function.
0
 

Author Comment

by:Zulma9999
ID: 7099089
Axter,
>>Moreover, you can accomplish the same thing by
>>calling ::Function(), which should also compile on both
>>compilers.

In the example I posted, this could fix the problem, but what if I have to parent classes with the same Function name, and I want to specify which base class function I want to use?

namespace WinX_Namespace
{
     class Widget1{
     public:
           bool Function(){return false;};
     };
     class Widget2{
     public:
           bool Function(){return true;};
     };
}

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
public:
 bool Function1();
 bool Function2();
};

//In cpp file
bool foo::Function1()
{
 return Widget1::Function();
}

//In cpp file
bool foo::Function2()
{
 return Widget2::Function();
}

Now I can't use ::Function call because the compiler is not going to know which one to pick.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7099127
In this case, you can try adding using namespace to your function.
Example:
bool foo::Function1()
{
     using namespace WinX_Namespace;
     return Widget1::Function();
}

That should make both VC++ happy and Apex happy.
0
 

Author Comment

by:Zulma9999
ID: 7099149
>>In this case, you can try adding using namespace to your
>>function.
That method would pull everything in the namespace, which I don't want to do.
I'm sorry to be so picky, but I'm trying to port some code, and I want to make sure it comes out right.

namespace WinX_Namespace
{
     class Widget1{
     public:
          bool Function(){return false;};
     };
     class Widget2{
     public:
          bool Function(){return true;};
     };
     bool SomeXFunction(){return false;}
}

bool SomeXFunction()
{
     return true;
}

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
public:
     bool Function1();
     bool Function2();
};

//In cpp file
bool foo::Function1()
{
     using namespace WinX_Namespace;
     bool x = SomeXFunction(); //Error here
     return Widget1::Function();
}

If I use your method I get this error from VC++
error C2668: 'SomeXFunction' : ambiguous call to overloaded function
0
 
LVL 30

Expert Comment

by:Axter
ID: 7099164
Ok, this is not going to be pretty, but it's the only method I can think of which will let your code compile on both compilers correctly.

When calling the base function, wrap {} around the function, and inside the {}, and before the function call, add a using namespace.
The curly cues will prevent the using namespace from pulling everything in the namespace into the entire function.  The using namespace stays within side of the scope of the curly cues {}
Example:

namespace WinX_Namespace
{
     class Widget1{
     public:
          bool Function(){return false;};
     };
     class Widget2{
     public:
          bool Function(){return true;};
     };
     bool SomeXFunction(){return false;}
}

bool SomeXFunction()
{
     return true;
}

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
public:
     bool Function1();
     bool Function2();
};

//In cpp file
bool foo::Function1()
{
     bool x = SomeXFunction();
     {
          using namespace WinX_Namespace;
          if (x) return Widget1::Function();
     }
     SomeXFunction();
     WinX_Namespace::SomeXFunction();
     {
          using namespace WinX_Namespace;
          return Widget1::Function();
     }
}

//In cpp file
bool foo::Function2()
{
     bool x = SomeXFunction();
     {
          using namespace WinX_Namespace;
          if (x) return Widget2::Function();
     }
     SomeXFunction();
     WinX_Namespace::SomeXFunction();
     {
          using namespace WinX_Namespace;
          return Widget2::Function();
     }
}

0
 

Author Comment

by:Zulma9999
ID: 7099949
Axter,
That works, but I like to keep this question open to see if any one else has a better method.
0
 
LVL 22

Expert Comment

by:ambience
ID: 7102674
what does your Apex compiler say about the following, VC doesnt complain..

namespace WinX_Namespace
{
    class Widget1{
    public:
          bool Function(){return false;};
    };

    class Widget2{
    public:
          bool Function(){return false;};
    };
}

typedef WinX_Namespace::Widget1 wid1;
typedef WinX_Namespace::Widget2 wid2;

class foo : public wid1, public wid2
{
public:
bool Function();
};

//In cpp file
bool foo::Function()
{
     wid1::Function();
     return wid2::Function();
}

0
 

Author Comment

by:Zulma9999
ID: 7103422
ambience,
Good try, but that puts wid1 & wid2 into the global namespace.  I'm trying to keep object in WinX_Namespace out of the global namespace.
0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 7103745
You could try the following in your class.

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
#ifdef  APEX
     typedef WinX_Namespace::Widget1 Widget1;
     typedef WinX_Namespace::Widget2 Widget2;
#endif//APEX
public:
     bool Function1();
     bool Function2();
};

Unfortunatly, these typedef's don't compile on VC++, but if it compiles in your Apex compiler, it should allow you to keep the rest of your code the same in both VC++ and Apex, and it should not effect the global namespace.
0
 
LVL 22

Expert Comment

by:ambience
ID: 7106723
Axter is right, in fact i anticipated that issue .. and i think i was going to suggest the same as Axter has mentioned.

Axter, i tried that

class foo : public WinX_Namespace::Widget1, public WinX_Namespace::Widget2
{
     typedef WinX_Namespace::Widget1 wid1;
     typedef WinX_Namespace::Widget2 wid2;
public:
bool Function();
};

and it compiled fine on VC++, no issues.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7107063
>>and it compiled fine on VC++, no issues.

Yes, it'll compile the way you have it, but the example I posted does not rename the base class.
It uses the same name, so you don't have to use a different name in your decedent class implementation.

That should fource the Apex compiler to work just like the VC++ compiler, in accepting code using just the class name with out the namespace.
0
 

Author Comment

by:Zulma9999
ID: 7153316
ambience, I posted additional points for you.

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20323276


Thank you both for your help, and sorry it took me so long to get back to this question.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question