Thread: I need help passing conditional values to a base constructor.

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    I need help passing conditional values to a base constructor.

    Hi everyone,

    Can someone tell me how can I pass conditional values to a base constructor?

    let say I have
    Code:
    class Base{
    
    Base(int a, int b, int c){
    }
    
    };
    
    class Derived :public Base{
    Derived(int x, int y, int z){
    }
    
    }
    
    I want if x=0;
    Derived(x,y,z) :Base(x,y,z){
    }
    if x>0
    Derived(x,y,z) :Base(x+2,y+2,z+2){
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can easily create a private member function (although, to be sure, you could make it static since the object hasn't been constructed yet).
    Pass along whatever parameters you need in your decisions to it, then have it return an appropriate value and initialize with it.
    Eg:

    Code:
    Derived(int x, int y, int z): Base(myfunc(x), myfunc2(x), myfunc3(x)) {}
    static int Derived::myfunc(int x) { /* Do something */ }
    //...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-15-2011, 12:31 PM
  2. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  3. Virtual base class constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 02:18 AM
  4. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 12:43 AM
  5. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 03:14 PM