Confused by this class
class ParaViewMainWindow::pqInternals : public Ui::pqClientMainWindow
{
public:
pqInternals()
{
}
};
What does it mean?
If it's something like class ParaViewMainWindow: public pqClientMainWindow I know that ParaViewMainWindow inherits from pqClientMainWindow, right?
But here it has ::pqInternals and later
pqInternals()
{
}
What exactly is this doing?
I know :: is the scope operator and I have use it, but never in this situation.
If you look at the definition of the class ParaViewMainWindow, it contains the declaration class pqInternals; (and most likely a member that is a pointer to pqInternals).
This is the definition of that class – its full name is ParaViewMainWindow::pqInternals – and it inherits from Ui::pqClientMainWindow, which is the class pqClientMainWindow in the namespace Ui.
Related
If I have a subclass which should only be instantiated by its parent class, is friend the appropriate method for accessing the constructor of the private or protected class?
To clarify, there are already questions where this is proposed as the answer. My question is specifically regarding whether this is the only answer and, if not, whether it is the most appropriate for this situation.
Example:
class Class_A {
public:
class Class_B {
// Adding 'friend' keyword here
friend class Class_A;
int _value;
Class_B(
int value)
:
_value(value)
{
}
};
protected:
static Class_A::Class_B createB(
int value)
{
return Class_B(value);
}
};
Credit goes to #Angew for correcting the first version of this answer. Here comes the update:
You are actually using the wrong term: Class_B is not a subclass of Class_B. The correct term is : nested class. The relationship implied by declaring one class inside another is the following one:
The nested class is a member of the enclosing one, and thus has the same access rights as a member (the nested class is basically an implicit friend of the enclosing class).
I.e. the nested class has access to protected and private members of the enclosing class, but not the other way around. Thus if you want to call a private or protected method (e.g. constructor) making them friends is the way to go.
guys, I encountered this problem on a tech blog,the question asked what are the correct solution to resolve the compiler error generated in the below code. I have searched for hours and can not get an answer.
class SomeClass
{
public:
int data;
protected:
class Nest
{
public:
int nested;
};
public:
static Nest* createNest(){return new Nest;}
};
void use_someclass()
{
SomeClass::Nest* nst = SomeClass::createNest();
nst->nested = 5;
}
A. Make function void use_someclass() a friend of class SomeClass.
B. Make the function createNest() a non-static function of SomeClass.
C. Declare the class Nest in public scope of class SomeClass.
D. Make the object nst a reference object, and make the function
createNest() return a Nest&.
E. Derive a class from SomeClass. Make the object nst a derived class
pointer so that it can access SomeClass's protected declarations.
C is certainly right and trival.
I believe A is also right, and espectially E is a classic way of doing this kind of things.
I want implement what is said in E, but have a few difficulites. (I hope someone can also implement the idea in A), below is my code:
class derived:public SomeClass{};
void use_someclass()
{
derived::Nest *nst=SomeClass::createNest();
nst->nested = 5;
}
in the above, the idea is we can access the Nest definition from a derived class.
in function use_someclass(), in the first line, the right hand side is a static function, and returns type Nest*, but on the left hand side, I don't know how to match the right hand side type. "derived::Nest" is wrong. compiler error: can not access protected member. Nest is only a definition in SomeClass, not member.
what can we use to replace "derived::Nest"? derived certainly saw the definition of Nest, but I don't know how to "say" it. Maybe somehow via "this" pointer.
You can change the visibility in your derived class:
class derived : public SomeClass {
public:
using SomeClass::Nest;
}
Let the following classes :
class BaseClass
{
class OnSomeEventListener
{
public:
enum SomeEnum { BLABLA }
virtual void OnSomeEvent( SomeEnum eventData ) = 0;
}
};
class ChildClass :
public BaseClass,
public BaseClass::OnSomeEventListener
{
virtual void OnSomeEvent( BaseClass::OnSomeEventListener::SomeEnum eventData );
}
My question is : why do I need to specify BaseClass:: in front of OnSomeEventListener::SomeEnum eventData in the method virtual void OnSomeEvent( BaseClass::OnSomeEventListener::SomeEnum eventData ); ?
If I don't do it, it says that OnSomeEventListener is ambiguous between BaseClass::OnSomeEventListener and BaseClass::OnSomeEventListener::OnSomeEventListener
Why would it think i'm referencing the constructor instead of the OnSomeEventListener type ? Why would i need to prefix the argument type with BaseClass:: since I'm already inside BaseClass ?
Thank you.
Why would i need to prefix the argument type with BaseClass:: since I'm already inside BaseClass ?
You are inside BaseClass, but you are also inside OnSomeEventListener because you inherit from both.
When the compiler parses a name, it doesn't think "I need a type here, can this be an enum?", instead it thinks "I have a name here, what can it be?". And in your case it can be two different things, depending on which base class is searched for the name.
You don't need the BaseClass::OnSomeEventListener scope at all since you bring that in with your inheritance of BaseClass::OnSomeEventListener:
class ChildClass:
public BaseClass,
public BaseClass::OnSomeEventListener
{
virtual void OnSomeEvent(SomeEnum eventData);
};
That being said OnSomeEventListener in the ChildClass is ambiguous since it can be either the constructor or the class from that scope since you inherit both.
When trying to use the name OnSomeEventListener::SomeEnum, it wasn't preceded with :: so the compiler uses the unqualified lookup rules to create a lookup set. And from the compiler error you can see it first only considers OnSomeEventListener which directly becomes ambiguous in that scope since both the class itself and the constructor matches.
I know the questions seems ambiguous, but I couldn't think of any other way to put it, but, Is it possible to do something like this:
#include<iostream>
class wsx;
class wsx
{
public:
wsx();
}
wsx::wsx()
{
std::cout<<"WSX";
}
?
Yes, that is possible. The following just declares wsx
class wsx;
That kind of declaration is called a forward declaration, because it's needed when two classes refer to each other:
class A;
class B { A * a; };
class A { B * b; };
One of them needs to be forward declared then.
In your example,
class wsx; // this is a class declaration
class wsx // this is a class definition
{
public:
wsx();
}
So yes, by using class wsx; it is possible to declare a class without defining it. A class declaration lets you declare pointers and references to that class, but not instances of the class. The compiler needs the class definition so it knows how much memory to allocate for an instance of the class.
This is the definition of the class
class wsx
{
public:
wsx();
}
This is the definition of the constructor
wsx::wsx()
{
std::cout<<"WSX";
}
THis is a forward declaration that says the class WILL be defined somewhere
class wsx;
Yes. But it is not possible to define a class without declaring it.
Because: Every definition is also a declaration.
You did define the class. It has no data members, but that's not necessary.
I'm not sure what you mean. The code you pasted looks correct.
I have a class that only really ever needed by classes in a certain class hierarchy. I wanted to know if it is possible to nest the class in the highest class's protected section and have all the other classes automatically inherit it?
"Inherit" is the wrong word to use since it has a very specific definition in C++ which you don't mean, but yes you can do that. This is legal:
class A {
protected:
class Nested { };
};
class B : public A {
private:
Nested n;
};
And code that is not in A or something that derives from A cannot access or instantiate A::Nested.