Reputation: 11
Let's say class A has members "int B" and "int C". When I create an object of this class, I would like to choose whether it will contain only "int B" or only "int C" (or maybe if it has them both).
(I know that an alternative is declaring B and C as std::vectors and then resizing them to 1, but that means I'll have to add [0] after them each time I use them, and that also means I'll have to waste 20 bytes per member, because it seems that std::vector uses 20B by default, whether you assign anything to it or not.)
Upvotes: 0
Views: 232
Reputation: 3298
Use inheritance. You may need some virtual methods too.
class Settlement
{
public:
virtual void updateMarketCount()=0;//pure virtual function
};
class Town : public Settlement
{
public:
virtual void updateMarketCount();
private:
int m_markets;
int m_industries;
};
class Village : public Settlement
{
public:
virtual void updateMarketCount();
private:
int m_markets;
};
This way you can assign all of these objects to a generic settlement pointer. For example:
std::shared_ptr<Settlement> mysettlementPtr(new Town());//C++ 11 only. Found in memory header
Settlement* mysettlementPtr = new Town();//C++ < 11
Here is how to do a vector:
std::vector<std::shared_ptr<Settlement>> mySettlements;
mySettlements.push_back(std::shared_ptr<Town>(new Town()));
mySettlements.push_back(std::shared_ptr<Village>(new Village()));
Here is a very simple working example:
Settlement.h:
#pragma once
class Settlement
{
public:
virtual void increaseMarkets()=0;
virtual int getMarketCount()=0;
};
Town.h
#pragma once
#include "Settlement.h"
class Town : public Settlement
{
public:
Town():m_markets(0){}
virtual void increaseMarkets(){++m_markets;}
virtual int getMarketCount(){return m_markets;}
private:
int m_markets;
int m_industries;
};
Village.h
#pragma once
#include "Settlement.h"
class Village : public Settlement
{
public:
virtual void increaseMarkets(){++m_markets;}
virtual int getMarketCount(){return m_markets;}
private:
int m_markets;
};
SimpleExample.cpp:
#include<iostream>
#include<memory>
#include "Settlement.h"
#include "Village.h"
#include "Town.h"
int main()
{
std::shared_ptr<Settlement> mysettlement(new Town());
std::cout << "I HAVE: " << mysettlement->getMarketCount() << " MARKETS" << std::endl;
mysettlement->increaseMarkets();
std::cout << "NOW I HAVE " << mysettlement->getMarketCount() << " MARKETS" << std::endl;
std::cout << "PRESS ENTER TO CONTINUE." << std::endl;
system("pause");
return 0;
}
NOTE: It is best to extract functions into .cpp files related to the headers.
Upvotes: 3
Reputation: 1549
How about this?
First, define 3 container classes that contain A, B or both.
class withA
{
int varA;
};
class withB
{
int varB;
};
class withAB
{
int varA;
int varB;
};
Next, define a template class, which is the class you want, and it contains this member variable, inside of which, there's A, B or A and B.
template<class T>
class theThing
{
T m_thingMember;
};
Next, this is how you declare it.
theThing<withA> thingObjA;
theThing<withB> thingObjB;
theThing<withAB> thingObjAB;
Upvotes: 0