Attribute Setting Methods

Discussion in 'C Programming' started by kjell@cablescan.com, Jan 16, 2008.

  1. Guest

    Hi,

    I wonder if someone may have a solution to a small problem I'm
    experiencing with a C++ program? I'm have two classes in my
    application that provides methods for setting parameters in the
    classes. These methods returns a pointer to the class itself so that
    you can use the return value to call another attribute setting method
    on the same line. This is what my program look like:

    #include <iostream>



    using namespace std;



    class CBase

    {

    public:

    CBase* SetParameterA(const int nA)

    {

    m_nA = nA;

    return this;

    };



    private:

    int m_nA;

    };




    class CSub: public CBase

    {

    public:

    CSub* SetParameterB(const int nB)

    {

    m_nB = nB;

    return this;

    };



    private:

    int m_nB;

    };




    main()

    {

    CSub* pMyInstance = new CSub();

    pMyInstance->SetParameterB(1)->SetParameterA(2);

    delete pMyInstance;

    }


    The problem is that if I call SetParameterA() before SetParameterB()
    I get a compiler error. I change the third line from the end to:

    pMyInstance->SetParameterA(2)->SetParameterB(1);


    When I compile the program I get the following error:

    > g++ test.cc

    test.cc: In function 'int main()':

    test.cc:37: error: 'class CBase' has no member named 'SetParameterB'


    This is because SetParameterA() returns a pointer to CBase and CBase
    does not have a method SetParameterB().

    So I'm trying to find a solution to this problem.

    One thing that I could do is make the method SetParameterA()
    polymorphic, override it in CSub, call the SetParameterA() method in
    base class and then type cast the return pointer. The problem with
    this approach is that I would have to write wrappers for all methods
    in the base class CBase in the derived class CSub.

    Would you by any chance have a more elegant solution to this problem?

    Thanks for you help,
    Kjell
    , Jan 16, 2008
    #1
    1. Advertising

  2. Al Balmer Guest

    On Tue, 15 Jan 2008 16:13:47 -0800 (PST), ""
    <> wrote:

    >I wonder if someone may have a solution to a small problem I'm
    >experiencing with a C++ program?


    You're more likely to get useful answers in comp.lang.c++.

    --
    Al Balmer
    Sun City, AZ
    Al Balmer, Jan 16, 2008
    #2
    1. Advertising

  3. "" <> writes:

    > I wonder if someone may have a solution to a small problem I'm
    > experiencing with a C++ program?


    You want comp.lang.c++ not comp.lang.c.

    --
    Ben.
    Ben Bacarisse, Jan 16, 2008
    #3
  4. wrote:
    > Hi,
    >
    > I wonder if someone may have a solution to a small problem I'm
    > experiencing with a C++ program?


    Someone in <news:comp.lang.c++> might. The _first_ thing to learn is
    that C and C++ are different languages.
    Martin Ambuhl, Jan 16, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bostonasian
    Replies:
    1
    Views:
    808
    Joris Gillis
    Sep 18, 2005
  2. Replies:
    2
    Views:
    1,065
    Henry S. Thompson
    Mar 6, 2006
  3. Guest
    Replies:
    6
    Views:
    382
    Guest
    Sep 4, 2007
  4. Replies:
    3
    Views:
    286
    James Kanze
    Jan 16, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    299
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page