0

Lets say I have a class with a private setup function. Is there a way to automatically call the function whenever I declare an object of this class?

bus.h:

class BUS
{
  private:
    void setup();
  public:
    void someOtherFunction(); 
};

main.cpp:

BUS bus; //automatically call setup() here
bus.someOtherFunction();
mrv
  • 311
  • 1
  • 4
  • 8
  • 1
    Just use a constructor, don't even declare a `setup` method. It's pretty counter-intuitive unless you need delayed initialization. – RamblingMad Oct 21 '15 at 13:43
  • 1
    Try looking into this list so that you get introduced into proper c++ , http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – g24l Oct 21 '15 at 14:01
  • " it is so big and complex, that there are very many very bad C++ books out there" - ohyeah, I got some of them :D – mrv Oct 21 '15 at 14:08

5 Answers5

2

This is what a constructor is for. If setup() should always be called when an object is created then we would use the constructor to call setup() so it is done without any user intervention.

class BUS
{
  private:
    void setup();
  public:
    Bus() { setup(); }
    void someOtherFunction(); 
};

Now BUS bus; will call setup().

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • yeah, that's it. apparently the things I read about c++ classes were too basic to mention putting functions in the constructor. now the concept of constructors totally makes sense to me. thanks a bunch – mrv Oct 21 '15 at 14:05
2

Just put your setup method in the default constructor, and if you need to call that in any other constructor; use a constructor initialization list:

class myclass{
    public:
        myclass(){ std::cout << "setup part" << std::endl; }
        myclass(int x): myclass(){ /* calls myclass() first */ }
};

int main(int argc, char *argv[]){
    myclass c0; // prints "setup part"
    myclass c1{5}; // also prints "setup part"
}

This is the most idiomatic way to do it.


If you need delayed initialization; I like to use a tagged constructor and an init method:

struct no_init_tag{};
no_init_tag no_init;

class myclass{
    public:
        myclass(int x){ init(x); }
        myclass(no_init_tag){}

        void init(int arg){ std::cout << "init with " << arg << std::endl; }
};

int main(int argc, char *argv[]){
    myclass c0{5}; // prints "init with 5"

    myclass c1{no_init}; // doesn't print
    int n = 0;
    // set 'n' somehow
    c1.init(n); // prints "init with " then 'n'
}

This makes it so that using dynamic memory for delayed initialization isn't required, but could still be done.

RamblingMad
  • 5,332
  • 2
  • 24
  • 48
1

All you need is a constructor:

class BUS
{
private:
void setup(){cout<<"Setup"<<endl;};
public:
BUS() // constructor
{
setup();  
}
void someOtherFunction(){cout<<"Something"<<endl;};
};

you can verify that by:

BUS bus; // will print Setup
vishal
  • 2,258
  • 1
  • 18
  • 27
0

Google C++ style guide is also very useful, you may have interesting to read it. From doing work in constructors section:

In general, constructors should merely set member variables to their initial values. Any complex initialization should go in an explicit Init() method. Doing work in constructors will have problems in some particular case, so if your object requires non-trivial initialization, consider having an explicit Init() method. In particular, constructors should not call virtual functions, attempt to raise errors, access potentially uninitialized global variables, etc.

Jack47
  • 194
  • 1
  • 8
  • A style guide may be what OP needs (I wouldn't recommend the Google style guide), but it does not answer the question. This would be far better suited as a comment. – RamblingMad Oct 22 '15 at 06:55
0

Simply use constructor, constructors have same name as class

#include <iostream>
class Bus
{
    private:
    Bus() {
    std::cout<<"Object created";
    }
};
int main()
{
    Bus h;
    return 0;
}

Result

Object created
Supergamer
  • 411
  • 4
  • 13