jramm
jramm

Reputation: 6655

C++ overloading; variable args of same type?

I have a class, whose constructor looks like this:

Chunker::Chunker(int chunkSizeX, int chunkSizeY){
    chunkX = chunkSizeX;
    chunkY = chunkSizeY;
}

I would like to offer the user the ability to have either chunkSizeY or chunkSizeX to have a default value (which needs to be calculated by Chunker).

I.E so they might pass in a 'AUTO' keyword or something so that the constructor knows.

Can I do something like:

Chunker::Chunker(int chunkSizeX, char chunkSizeY)

Chunker::Chunker(char chunkSizeX, int chunkSizeY)

Chunker::Chunker(char chunkSizeX, char chunkSizeY)

So that if it gets a char for one or both of the values, it knows to auto-calculate them?

I'm sure their must be a better/standard way I don't know of yet....?

Upvotes: 1

Views: 100

Answers (4)

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17424

I'd suggest Boost.Optional, which will give you code like this:

void function(optional<int> x, optional<int> y) {
    if (x) {
        // use *x
    }
    if (y) {
        // use *y
    }
}

That said, for the case of constructors, it's sometimes a good idea to add a static factory function in the class:

class Chunker {
    static Chunker create_with_x(int x) {
        ...
    }
    static Chunker create_with_y(int y) {
        ...
    }
};

Upvotes: 2

Jarod42
Jarod42

Reputation: 217850

You may use a special empty class to help:

struct Default {};

class Chunker
{
public:
    Chunker(int x, int y) : chunkX(x), chunkY(y) {}
    Chunker(Default, int y) : chunkX(42), chunkY(y) {}
    Chunker(int x, int Default) : chunkX(x), chunkY(42) {}

private:
    int chunkX;
    int chunkY;
};

usage:

Chunker c(1, 2);
Chunker cx(3, Default{});
Chunker cy(Default{}, 4);

or use "static builder"

class Chunker
{
public:
    Chunker(int x, int y) : chunkX(x), chunkY(y) {}
    static Chunker FromX(int x) { return Chunker(x, 42); }
    static Chunker FromY(int y) { return Chunker(42, y); }
private:
    int chunkX;
    int chunkY;
};

usage:

Chunker c(1, 2);
Chunker cx = Chunker::FromX(3);
Chunker cy = Chunker::FromY(4);

Upvotes: 5

Math
Math

Reputation: 2446

You can specify multiple constructors in C++ as long as they have different signatures, and the appropriate constructor will be selected by the compiler for each call.

So you can perfectly do :

In Chunker.cpp

Chunker::Chunker(int chunkSizeX, char chunkSizeY) {...} // First constructor
Chunker::Chunker(char chunkSizeX, int chunkSizeY) {...} // Second constructor
Chunker::Chunker(char chunkSizeX, char chunkSizeY) {...} // Third constructor

Then in another file of your program:

Chunker::chunker chunker1 = Chunker::Chunker(1, 'b') // First constructor is used
Chunker::chunker chunker2 = Chunker::Chunker('a', 2) // Second constructor is used
Chunker::chunker chunker3 = Chunker::Chunker('a', 'b') // Third constructor is used

Upvotes: 0

Paul Evans
Paul Evans

Reputation: 27577

Simply write a helper class that builtds Chunkers, something like:

class MakeChunker {

    void setX(int x) { ...
    void setY(int y) { ...
    Chunker make()

Upvotes: 4

Related Questions