C++ why is `this` necessary? [duplicate] - c++

This question already has answers here:
Derived template-class access to base-class member-data
(3 answers)
Why do I have to access template base class members through the this pointer?
(3 answers)
Closed 10 months ago.
With GCC, we can't access the member in the base class without writing this explicitly, but it works on MSVC, what is going on? Is it because of the CRTP?
#include <iostream>
template<class T>
struct Base {
protected:
T* a;
};
template<class U>
struct Derived : Base<Derived<U>> {
void print_a() {
std::cout << a << std::endl; // doesn't work on GCC
std::cout << this->a << std::endl; // works on GCC
}
};
int main() {
Derived<float> d;
d.print_a();
}

Related

Call virtual function on derived member variable [duplicate]

This question already has answers here:
Initialization Order of Class Data Members
(2 answers)
Closed 11 months ago.
This little test program crashes and I'm interested why it does:
#include <iostream>
struct SomeClass {
SomeClass() {
}
virtual ~SomeClass() {
}
void test() {
std::cout << "test" << std::endl;
}
virtual void onInit() {
std::cout << "test" << std::endl;
}
};
struct Base {
Base(SomeClass *ptr) {
ptr->test();
ptr->onInit();
}
};
struct Derived : Base {
SomeClass cls;
Derived() : cls(), Base(&cls) {
}
};
int main(int, const char **) {
Derived test;
}
Why can't I call the virtual function onInit from the base class? Isn't that fully initialized when I use cls() in the initializer list?
Base classes and members are initialized in the order of declaration in the class and not in the order you put in the initialization list.
Bases are always initialized before members, so the Base's constructor dereferences a pointer to an uninitialized object, which is undefined behavior.

Why in c++, a derived class template publicly Inherited from base class template can't call the base class' funtion directly? [duplicate]

This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
Accessing public inherited Template data members [duplicate]
(1 answer)
Closed 2 years ago.
This is the code:
class A : public std::vector<int>
{
public:
void fooA()
{
std::cout << "A::fooA() + " << size() << '\n';
}
};
template <typename T>
class B : std::vector<T>
{
public:
void fooB()
{
std::cout << "B::fooB() + " << size() << '\n';
}
};
Class A is correct.
But in class B, size() is considered undeclared, unless I change it to std::vector<T>::size()
Why doesn't class A have to add std::vector<int>, but does class B have to?

Creating an std::vector<A&> and fillng it with classes derived from A [duplicate]

This question already has answers here:
Why can't I make a vector of references?
(11 answers)
stl container of references C++11
(4 answers)
Closed 3 years ago.
I want to create an std::vector<A&> and fill it with elements of class B and C, which derive from A.
struct A {
virtual void print (){std::cout << "A" <<std::endl;}
};
struct B : public A {
void print (){std::cout << "B" <<std::endl;}
};
struct C : public A {
void print (){std::cout << "C" <<std::endl;}
};
int main()
{
B b;
C c;
std::vector<A&> vec{b,c};
for(auto &e : vec)
{
e.print();
}
return 0;
}
I know it can be done with ptr but my question if for reference

Inheriting protected fields in template derived class of template class [duplicate]

This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
Derived template-class access to base-class member-data
(3 answers)
Using member variables inherited from a templated base class (C++)
(1 answer)
Closed 3 years ago.
I'm having issues with accessing protected members in template subclasses of a template class.
Thing is, if I remove the templates, everything works fine. I know how to work around my problem in an ugly way, by simply writing "using Base::my_protected_member" for every single derived class. But why should I need to do that in the first place ?
Here's a minimal commented example
template<class T>
class Base
{
protected:
T x;
public:
Base(T x) : x(x) {}
};
template<class T>
class Derived : public Base<T>
{
//using Base<T>::x; //why ?!
public:
Derived(T x);
void print();
};
template<class T>
Derived<T>::Derived(T x) : Base<T>(x)
{ cout << x << endl; } //works fine
template<class T>
void Derived<T>::print() { cout << x << endl; } //doesn't work
int main()
{
Derived<int> d(4);
d.print();
}
I don't quite understand what's going on, or how to work around this in a nice way. probably I'm doing something wrong...

C++ calling parent function only works when using a pointer [duplicate]

This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Closed 9 years ago.
Of these two programs the second one works but the first one does not compile. How is that possible? The only difference is that in version two bar is a pointer and in version one it isnt.
Version one: (does not compile)
#include <iostream>
class Foo{
public:
void print(){
std::cout << "asdasd" << std::endl;
}
};
class Bar : public Foo{
};
int main(){
Bar bar();
bar.print();
}
And the second version:
#include <iostream>
class Foo{
public:
void print(){
std::cout << "asdasd" << std::endl;
}
};
class Bar : public Foo{
};
int main(){
Bar* bar = new Bar();
bar->print();
}
Bar bar();
is a declaration of a function.
Bar bar;
is your friend.