user6241091
user6241091

Reputation:

Constant member in inherited class c++

Big edit:

I have a code in which I have to add a constant member in a inherited class by using _elemente (which is a vector). Not to add a member in the inherited classes, just by using _elemente. In every inherited classes (let's say B, C, D and E) I withh have MAX_VAL1, MAX_VAL2 and so on with different values. I tried:

#include <iostream>
#include <iomanip>
#include <vector>
typedef unsigned int Uint;
typedef vector<Uint> TVint;
typedef vector<Uint>::const_iterator TIterator;

class A
{
protected:
Uint _count;
TVint _elemente;
public:
//
};

class B : public A
{
    const int MAX_VAL;
};

But it has a member and I don't have to have a member in the inherited class.

All the code here:

.h: http://pastebin.com/P3TZhWaV
.cpp: http://pastebin.com/ydwy2L5a

The work from the inherited classes is done using that constant members.

if MAX_VAL1 < count
{
throw Exception() {}
}
 if (_elemente.size() == 0) // As _elemente is a vector from STL
{
    _elemente.push_back(0);
}
for (int i = _elemente.size(); i < count; i++)
{
    _elemente.push_back(_elemente[i * (i+1) / 2]);
}
}

I don't think that is correct as I have to use the Vector from STL and I don't really think that is the way the constant member from a inherited class without the actual member declared should be added. Thanks for your help.

Upvotes: 2

Views: 2551

Answers (4)

56ka
56ka

Reputation: 1575

If you want to access it statically, you can do it by using templates :

  • ABase gives polymorphic access to value
  • A gives static access to value
  • B and Care examples of usage

.

// This is the polymorphic root class
class ABase
{
public:
    ABase(int maxV) : _maxV(maxV) {}
    int maxValue() { return _maxV; }
private:
    int _maxV;
};

// This class gives static method
template<int V_MaxValue>
class A : public ABase
{
public:
    A() : ABase(V_MaxValue) {}
    static int maxValue() { return V_MaxValue; }
};

class B : public A<42>
{
};

class C : public A<35>
{
};

// Static access (neex explicit class call) :
// B::maxValue() => 42
// C::maxValue() => 35
//
// Polymorphic call :
// ABase* poly = new B();
// poly->maxValue() => 42

Upvotes: 0

RyanP
RyanP

Reputation: 1918

Based on other comments it seems like you want a const number that is accessible in the base class which can have a different value depending on the derived class. You could achieve that like this: https://ideone.com/JC7z1P

output: A: 50 B: 80

#include <iostream>
using namespace std;

class Base
{
private:
    const int aNumber;
public:
    // CTOR
    Base( const int _aNumber ) :
        aNumber( _aNumber ) {}

    // check value  
    int getNumber() const
    {
        return aNumber;
    }
};

class A : public Base
{
public:
    A() : Base( 50 ) {}
};

class B : public Base
{
public:
    B() : Base( 80 ) {}
};

int main() {
    A a;
    B b;

    std::cout << "A: " << a.getNumber() << std::endl;
    std::cout << "B: " << b.getNumber() << std::endl;

    return 0;
}

Upvotes: 1

Rob K
Rob K

Reputation: 8926

You could use a virtual function, something like this:

    class A
    {

        virtual int max_val() const = 0;

        protected:
        Uint _count;
        TVint _elemente;

        public:
        //
    };

    class B : public A
    {
        int max_val() const { return 42; }
    };


    if ( max_val() < _count ) ...

Upvotes: 1

Alexander Enaldiev
Alexander Enaldiev

Reputation: 76

When you write like

class B : public A
{
    const int MAX_VAL;
};

what value do you expect B's class instance to hold with current approach? Have you tried to add ctor to B (to initialize MAX_VAL to some exact value), so that whole class definition should be like

class B : public A
{
    const int MAX_VAL;
public:
    B(int max_val):MAX_VAL(max_val) {}
};

Also, the code above shows a lot of unanswered questions. Some of them:

  • Do you really need it to be member? mark it as 'static' (static const int MAX_VAL = 5) . That would mean, every B's instance MAX_VAL would be equal
  • All of type redifinitions don't look meaningful. What if you use intrisic types and auto?
  • Usually one doesn't compare size() with 0 - just calls empty().

Have you tried to read Stroustrup or Lippman?

Upvotes: 0

Related Questions