I came across code with the following format:
class A {
typedef struct {
// variables of struct B
} B
// private and public variables, member functions of class A
}
What is the point of putting a struct in a class instead of outside of it ?
And who gets to access the struct if it is placed in a class ?
Only the class or can another class use it too ?
Thank you in advance :D
in C++, an 'inside' class is essentially under a 'namespace' of the outer class.
That is, you access it using A::B from anywhere, as long as it is public.
It's not like Java where the inner class is related to a pointer of the outer class. You don't need an A* to have an A::B*.
They have no low-level connection and the inner class cannot see anything in the outer class without a given outer-class pointer.
Related
class Containing{
class Subclass{
Containing cont;
};
};
Currently this says "error: field cont has incomplete type". Is this not possible at all with C++? Or when someone needs this does it mean they have wrong program design? Should Subclass be a derived / separate class instead?
This is the same problem as
class Containing;
class Subclass
{
Containing cont;
};
You can solve it in the same way as the non-nested problem, by defining the dependent class after the class it depends on.
class Containing
{
class Subclass;
};
class Containing::Subclass
{
Containing cont;
};
I think what you want could be a pointer:
class Containing;
class Containing{
class Subclass{
Containing* cont;
};
};
In Java, class instances are pointers by default. This means that this is just an address value which contains the address of the Containing object. So the cont is a separate instance which you refer to by the pointer. Your code doesn't work because the declaration of Containing isn't finished, thus Containing isn't known yet. It would need its own declaration to finish its own declaration.
What I did above the class is a forward declaration. This means I told the compiler that there is a class with the name Containing declared. Then you can use a pointer to Containing, because you don't have to know anything about the members or the size of Containing.
Be aware, that you by default have no pointer, you first need an instance of Subclass, maybe this is even what you want:
class Containing;
class Containing{
Containing* cont;
};
I have created two classes A and B where B inherits from class A. As you can see, I have a vector in class A that is currently under the protected section of the class. I am unsure if using protected is bad practice?
#include <vector>
class A
{
public :
A();
protected:
std::vector <std::string> a;
};
class B : A
{
public :
B();
void accessVector()
{
a.size();
}
private:
};
When A makes a data member a protected, it is offering the following guarantee to all classes that derive from it:
"You may do anything you like to a without telling me. This includes appending to it, modifying its contents, removing items, sorting it, moving from it, moving to it and otherwise making its state undefined and/or unknowable to me".
Remember that anyone may create a class that derives from A.
For this reason, to all intents and purposes, a protected member is a public member, since a derived class may simply say the following:
public:
using A::a;
Starting here and working forward, you'll find that there are only two sensible use-cases for protected:
When a base class defines a virtual member function that may need to be called from an overridden version of the same function in a derived class.
When the base class wants to expose 'data as interface' to a derived class, but not to the world.
so i have a user class which has two subclasses: manager and regular_User and i want manager to be singleton.
can anybody help me how to do it?
thanks!
class user{
public:
//sth
protected:
//sth
};
class manager:public user{ //i want this to be singleton
//
};
class regular_user:public user{
};
Although I question why you'd want manager to be a singleton, you could achieve something by using an anonymous class (or struct) with a single instance:
struct user {
static struct {
} manager;
};
The fun bit is allocating storage for the anonymous struct, since you don't know its type! You need to do this otherwise you'll get link-time errors. But C++11 has a way. Include
namespace {
decltype(user::manager) user::manager;
}
in exactly one compilation unit.
First of all, to make the class Manager a subclass of class User, you need to declare it with something like class Manager : public User. Then to make class Manager a Singleton class, you need to guarantee that only one instance of that class (one object) can be created. If you make all of its class member variables and member functions static, then those members will only get created once.
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;
}
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.