Click here to Skip to main content
15,887,331 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to get a C++ console application run in a terminal/powershell type of environment? Pin
Richard MacCutchan1-Oct-12 21:25
mveRichard MacCutchan1-Oct-12 21:25 
GeneralRe: How to get a C++ console application run in a terminal/powershell type of environment? Pin
infectedprof1-Oct-12 21:48
infectedprof1-Oct-12 21:48 
GeneralRe: How to get a C++ console application run in a terminal/powershell type of environment? Pin
Richard MacCutchan2-Oct-12 2:51
mveRichard MacCutchan2-Oct-12 2:51 
Questionproblem Pin
emanalshboul30-Sep-12 9:42
emanalshboul30-Sep-12 9:42 
AnswerRe: problem Pin
Albert Holguin1-Oct-12 4:09
professionalAlbert Holguin1-Oct-12 4:09 
QuestionGeneric Get and Set as interface Pin
Durga_Devi30-Sep-12 1:13
Durga_Devi30-Sep-12 1:13 
AnswerRe: Generic Get and Set as interface Pin
CPallini30-Sep-12 3:03
mveCPallini30-Sep-12 3:03 
AnswerRe: Generic Get and Set as interface Pin
pasztorpisti30-Sep-12 5:10
pasztorpisti30-Sep-12 5:10 
One generic solution that might be suitable has already been posted by CPallini. Another one more C++ish solution could be something like this:
Define a base class for your different types:
C++
class CValue
{
};

Derive a class for all types you support:
C++
class CStringValue : public CValue
{
public:
    // CStringValue specific methods like void SetValue(const std::string&) and
    // const std::string& GetValue();
private:
    std::string m_Value;
};

class CIntValue : public CValue
{
public:
    // CIntValue specific methods like void SetValue(int) and
    // int GetValue();
private:
    int m_Value;
};

The advantage of this solution that your data type can be anything, it can contain an int array, or one of your own classes if you want and not just 'primitives'. The derived class itself can be a type put together by compositing other types... Your interface methods should look like the following:
C++
void SetValue(CValue* val);
CValue* GetValue();

We reached the point where we want to use the actual value but inside SetValue() all we have is a CValue* that is useless in its current form so we put in some useful virtual methods in this base class. First I want to be able to decide the exact type of the value. For this reason I usually use one or both of the following methods:
1. introducing an enum that contains a member for each supported possible datatype and you can query it from the base class.
2. virtual downcast methods in the base class.
C++
class CStringValue;
class CIntValue;

class CValue
{
public:
    enum EType
    {
        EType_String,
        EType_Int,
    };
    virtual Etype GetType() = 0;

    // Downcast methods, not the most beautiful OO solution of the world but sometimes
    // in a fixed size class hierarchy like this it is acceptable and comfortable, and
    // more beautiful than using for example C++'s dynamic_cast. If quite comfortable
    // to say for example "if (CStringValue* sv = val->AsStringValue())"

    // override this only in CStringValue and put "return this;" into the method body.
    virtual CStringValue* AsStringValue() { return NULL; }
    // override this only in CIntValue and put "return this;" into the method body.
    virtual CIntValue* AsIntValue() { return NULL; }
};

Note that both the type enum and downcast methods are against the dynamic extensibility of the class hierarchy from outside world, other libraries. In some cases this isn't a problem at all, it depends on the actual problem. You might omit this whole enum + downcast method thingy as it is. You might be OK with a few other kind of virtual methods that follow:
You can put in for example some kind of serialization/deserialization methods. One often used handy serialization format is the string! Smile | :)
C++
class CValue
{
public:
    virtual std::string ToString() const = 0;
    virtual bool SetFromString(const std::string& s) = 0;

    // OK, lets support XML too...
    virtual void SaveToXml(XmlElement* node) = 0;
    virtual bool LoadFromXml(XmlElement* node) = 0;

    // TODO: put here your other useful utility virtual methods
};


EDIT: In case of fixed type hierarchy the visitor pattern might come handly depending on the kind of the problem.
C++
class CValueVisitor;
class CValue
{
public:
    virtual void accept(CValueVisitor* visitor);
};

class CValueVisitor
{
public:
    virtual void visit(CStringValue* v) = 0;
    virtual void visit(CIntValue* v) = 0;
};

If you use visitor then you can implement even the aformentioned serialization methods as visitors. Whether to use virtual methods in your base class, or to use visitor instances depends on your actual problem.

modified 30-Sep-12 11:39am.

GeneralRe: Generic Get and Set as interface Pin
Durga_Devi30-Sep-12 8:13
Durga_Devi30-Sep-12 8:13 
GeneralRe: Generic Get and Set as interface Pin
pasztorpisti30-Sep-12 8:30
pasztorpisti30-Sep-12 8:30 
GeneralRe: Generic Get and Set as interface Pin
Durga_Devi30-Sep-12 22:13
Durga_Devi30-Sep-12 22:13 
GeneralRe: Generic Get and Set as interface Pin
pasztorpisti30-Sep-12 22:59
pasztorpisti30-Sep-12 22:59 
GeneralRe: Generic Get and Set as interface Pin
Durga_Devi3-Oct-12 10:14
Durga_Devi3-Oct-12 10:14 
GeneralRe: Generic Get and Set as interface Pin
pasztorpisti3-Oct-12 10:30
pasztorpisti3-Oct-12 10:30 
QuestionCrystal Report Win32 C++ in VS2010 Pin
simuytrix28-Sep-12 21:51
simuytrix28-Sep-12 21:51 
AnswerRe: Crystal Report Win32 C++ in VS2010 Pin
Richard MacCutchan28-Sep-12 22:11
mveRichard MacCutchan28-Sep-12 22:11 
AnswerRe: Crystal Report Win32 C++ in VS2010 Pin
Davide Zaccanti1-Oct-12 10:15
Davide Zaccanti1-Oct-12 10:15 
QuestionMigration from VC2006 to 2008 Pin
Stan the man28-Sep-12 15:55
Stan the man28-Sep-12 15:55 
AnswerRe: Migration from VC2006 to 2008 Pin
Richard MacCutchan28-Sep-12 22:10
mveRichard MacCutchan28-Sep-12 22:10 
GeneralRe: Migration from VC2006 to 2008 Pin
Stan the man28-Sep-12 23:23
Stan the man28-Sep-12 23:23 
GeneralRe: Migration from VC2006 to 2008 Pin
Richard MacCutchan28-Sep-12 23:33
mveRichard MacCutchan28-Sep-12 23:33 
GeneralRe: Migration from VC2006 to 2008 Pin
Marius Bancila1-Oct-12 10:29
professionalMarius Bancila1-Oct-12 10:29 
AnswerRe: Migration from VC2006 to 2008 Pin
CPallini29-Sep-12 3:46
mveCPallini29-Sep-12 3:46 
GeneralRe: Migration from VC2006 to 2008 Pin
Stan the man29-Sep-12 18:51
Stan the man29-Sep-12 18:51 
GeneralRe: Migration from VC2006 to 2008 Pin
CPallini30-Sep-12 3:03
mveCPallini30-Sep-12 3:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.