Thread: constructor.

  1. #1
    Unregistered
    Guest

    Unhappy constructor.

    Could anybody please help me out -

    <code>
    class CltmRsltDbltBase : public CltmGrpBase {
    public:
    /* Constructor */
    CltmRsltDbltBase() : CltmGrpBase(), m_profile( NULL ) {};

    :
    :
    :
    }
    </code>

    I want to change this to something like -
    <code>
    class CltmRsltDbltBase : public CltmGrpBase {
    public:
    /* Constructor */
    CltmRsltDbltBase() : CltmGrpBase(), m_profile( NULL ) {
    maxFields = CLTM_MAX_RSLT_LEN;
    };
    CltmRsltDbltBase(Int maxFlds) : CltmGrpBase(), m_profile( NULL ) {
    maxFields = maxFlds;
    };
    private:
    Int maxFields; //Cant assign a value here since the compiler warns (that it is not ANSI C++ compliant. - No I cant.)
    </code>
    I want to use maxFields within some members of the class CltmRsltDbltBase().

    The CltmRsltDbltBase is inturn used to define another (new) class CltmRsltTapLengthTable.

    <code>
    class CltmRsltTapLengthTable : public CltmRsltDbltBase {
    public:
    /* Constructor */
    CltmRsltTapLengthTable(Int maxFlds) : CltmRsltDbltBase(maxFlds) {};
    :
    :
    </code>

    The class is instantiated somewhere down the line as
    <code>
    new CltmRsltDbltBase (MAX_TAPS);
    </code>

    So what I want to do is that I want to slightly change some behaviour within the member of CltmRsltDbltBase using the newly introduced maxFields value. Earlier a constant (#define) was used in this place. However for the new class CltmRsltTapLengthTable I need the value to be different from the existing constant. The behaviour of other classes already derived from CltmRsltDbltBase should not change.

    As you would have already gathered I am a C programmer trying to fix C++ code!

    Please dont suggest drastic design changes since it is an existing code base with a lot of running
    and stable systems. I wont be allowed to change the design.

    Any help would be greatly appreciated.

    Thanks.

    PS: I tried using my minimal knowledge about default values instead of 2 (overloaded) constructors - but that didnt work either.

  2. #2
    Unregistered
    Guest
    Sorry small correction - the instantiation will be like

    new CltmRsltTapLengthTable(MAX_TAPS)

    and _not_ as mentioned in the post.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    The code that you want to change it to looks good to me.
    Have you tested it?

    Since maxFields is a private var, the only class that can access it directly (ie without using helper functions) is the class it is defined in. So in the constructor you should be able to set it.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Code:
    CltmRsltDbltBase() : CltmGrpBase(), m_profile(NULL ) {
        maxFields = CLTM_MAX_RSLT_LEN;
    };//Dont need this semi-colon
    CltmRsltDbltBase(Int maxFlds) : CltmGrpBase(), m_profile( NULL ) {
        maxFields = maxFlds; 
    }; //Nor do you need this one
    See the semi-colon's, don't need them. It just like a normal block of code. You only need the semi-colon's at the end of a class declaration.

    Code:
    class cName {
    //...Stuff
    };
    Okay

    kwigibo
    Last edited by kwigibo; 05-31-2002 at 06:13 PM.

  5. #5
    Unregistered
    Guest

    This works, thanks all.

    class CltmRsltDbltBase : public CltmGrpBase {
    public:
    /* Constructor */
    CltmRsltDbltBase(Int maxFlds=CLTM_MAX_RSLT_LEN):CltmGrpBase(),m_profile (NULL
    ){
    maxFields = maxFlds;
    };


    :
    :
    class CltmRsltTapLengthTable : public CltmRsltDbltBase {
    public:
    /* Constructor */
    CltmRsltTapLengthTable() : CltmRsltDbltBase(MAX_TAPS) {};
    :
    :

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM