C++, seeing objects in base template class from derived template class [duplicate] - c++

This question already has answers here:
templates: parent class member variables not visible in inherited class
(3 answers)
Closed 8 years ago.
When I compile the following templated C++ code with GCC 4.8.3
template <typename dtype> class Base {
public:
dtype base;
dtype ceiling;
Base() { };
virtual ~Base() { };
};
template<typename dtype> class Building : public Base<dtype> {
public:
dtype wall;
Building(dtype concrete) {
Base<dtype>::base=concrete;
ceiling=concrete;
wall=concrete;
};
~Building() { };
};
int main (int argc, char* argv[]) {
Building<float>* building=new Building<float>(2.0);
std::cout << building->base << std::endl;
}
I get the error
error: ‘ceiling’ was not declared in this scope
ceiling=concrete;
So it appears that
Base<dtype>::base=concrete;
works, but
ceiling=concrete;
does not. Is there any way I can mogrify this templated code so that, in the derived class constructor I can just reference "ceiling" from the templated base class without having to clarify which class it is from?
Thanks in advance

You can use this->ceiling.

Related

child class not able to access parent member [duplicate]

This question already has answers here:
Derived template-class access to base-class member-data
(3 answers)
Closed 5 years ago.
I have created a variable in base class that is a vector of template but I'm not able to access this member from the derived class , can someone explain?
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
/*
*
*/
template <typename T>
class Apple{
protected:
public:
vector<T> vec;
virtual void foo(T node) =0;
};
template <typename T>
class Ball:public Apple<T>{
public:
void foo(T node){
cout<<"hahaha\n";
vec.push_back(node);/* I GET COMPILATION ERROR HERE"vec was not declared in this scope"*/
}
};
int main(int argc, char** argv) {
Ball<int> b;
b.foo(10);
return 0;
}
The member vec is of a template parameter dependent type so the compiler needs some help like:
this->vec.push_back(node);
or use qualified name:
Apple<T>::vec.push_back(node)
Thanks to #Avran Borborah - Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class?
Here’s the rule: the compiler does not look in dependent base classes (like B<T>) when looking up nondependent names (like f).
This doesn’t mean that inheritance doesn’t work. ...
You have not created any private members in the base class.
To resolve the error, you can replace the line which produces compilation error with :
Apple::vec.push_back(node);
or
this->vec.push_back(node)

Unable to compile code calling static functions on different classes [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 5 years ago.
Let's suppose I have two classes (actually more, but only 2 in my MCVE) defining two static functions with exactly the same name:
class A
{
public:
static void doSomething() {};
static void doSomethingElse() {};
};
class B
{
public:
static void doSomething() {};
static void doSomethingElse() {};
};
I want to call one of those functions for all available classes. So I created a helper function:
template<class Helper> static void ApplyToAllTypes( Helper& helper )
{
helper.apply<A>();
helper.apply<B>();
}
Then I do this to call doSomething on all classes:
class doSomethingHelper
{
public:
template<class T> static void apply()
{
T::doSomething();
}
};
void doSomethingToAll()
{
doSomethingHelper helper;
ApplyToAllTypes<doSomethingHelper>( helper );
}
And this to call doSomethingElse on all classes:
class doSomethingElseHelper
{
public:
template<class T> static void apply()
{
T::doSomethingElse();
}
};
void doSomethingElseToAll()
{
doSomethingElseHelper helper;
ApplyToAllTypes<doSomethingElseHelper>( helper );
}
It works fine when compiled with MSVC, but when I try to compile this with g++, it complains:
In static member function 'static void ApplyToAllTypes()':
error: expected '(' before '>' token
helper.apply<A>();
Is that really invalid? Should the sytax be fixed in any way or do I need to find an alternative (then proposed alternative would be appreciated)?
You have to write
helper.template apply<A>();
Visual Studio accepts this (wrong) syntax, though.

Template Inheritance member fields [duplicate]

This question already has answers here:
Accessing inherited variable from templated parent class [duplicate]
(2 answers)
Closed 7 years ago.
I have something that i can't understand.
With one basic template class:
#ifndef DBUFFER_HPP
#define DBUFFER_HPP
#include <memory>
namespace memory {
template <template <typename T, class Alloc = std::allocator<T> > class Stock, class Unit>
class DBuffer {
typedef Stock<Unit> buffer_t;
protected:
const std::size_t m_sizeMax;
std::unique_ptr<buffer_t> m_data;
std::unique_ptr<buffer_t> m_backData;
public:
DBuffer(const std::size_t sizeMax) : m_sizeMax(sizeMax),
m_data(new buffer_t()),
m_backData(new buffer_t()) {}
virtual ~DBuffer() = default;
public:
const buffer_t& current() { return *m_data; }
void swap() { m_data.swap(m_backData); }
};
}
#endif
I just want to inherit from it, but :
#ifndef VIDEO_BUFFER_HPP
#define VIDEO_BUFFER_HPP
#include "dbuffer.hpp"
#include <deque>
namespace video {
template <typename T>
class VideoBuffer : public memory::DBuffer<std::deque, T> {
private:
static const unsigned int VIDEO_FPS_MAX = 60;
public:
VideoBuffer() : memory::DBuffer<std::deque, T>(VIDEO_FPS_MAX){}
~VideoBuffer() = default;
private:
void pop_to_back() {
m_backData->push_front(std::move(m_data->front()));
if (m_backData->size() > m_maxSize)
m_backData->pop_back();
m_data->pop_front();
}
#endif
But the only error is a not declared on every member that i tried to call from the base class.
Maybe i have a problem because some template type is not specified?
If somebody can explain why, i'll be thanks full.
To resume comment:
You have to replace private: by protected: to access parent members and inform that the members are dependant name: (for example by using this->)
void pop_to_back() {
this->m_backData->push_front(std::move(this->m_data->front()));
if (this->m_backData->size() > this->m_maxSize)
this->m_backData->pop_back();
this->m_data->pop_front();
}

what's the situation that you must use "this" pointer? [duplicate]

This question already has answers here:
Is there any reason to use this->
(16 answers)
Closed 7 years ago.
I have a code working like:
class base {
....
protected:
typeA m_mem;
}
class mymodule: public base{
....
void function(){
m_mem.call();
}
}
This was working OK before. Suddenly, I see it brokes saying "m_mem was not declared...." It might be some other people changed the namespace or other parts.
And I found it was working by just adding "this" then it compiles fine
this->m_mem.call()
While I just would like to know what's the cases that I must use "this" ? I learned "this" can be used to point to distinguish between class member and argument names. While for my case, what can be the reason that I must use "this" for accessing a data member
This can occur for example when you use templates.
The compiler will issue an error that x is an undefined variable for these class definitions
template <class T>
struct A
{
int x;
};
template <class T>
struct B : A<T>
{
void set_x( int v ) { x = v; }
};
If you write
void set_x( int v ) { this->x = v; }
the code will compile.

How to make a class Non-Inheritable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Making a template parameter a friend?
C++ Faq 23.11 How can I set up my class so it won't be inherited from?
lists the following code:
class Fred;
class FredBase {
private:
friend class Fred;
FredBase() { }
};
class Fred : private virtual FredBase {
public:
...
};
I tried to make a generic template for the same.
#include <stdio.h>
template<typename MAKE_ME_NONINHERITABLE >
class NonInheritable{
private:
NonInheritable(){
}
friend MAKE_ME_NONINHERITABLE; //<--- error here
};
This give me an error:
xxx.cpp:11: error: a class-key must be used when declaring a friend
So I tried:
template<typename MAKE_ME_NONINHERITABLE >
class NonInheritable{
private:
NonInheritable(){
}
friend class MAKE_ME_NONINHERITABLE; //<--- error here
};
class A : virtual public NonInheritable<A>{
};
And I get this error:
xxx.cpp:11: error: using typedef-name `MAKE_ME_NONINHERITABLE' after `class'
Is there a way to make this work?
You can use final from c++11 or sealed from microsoft extensions for c++.