2

I'm trying to create an object which I want to populate inside a switch-case, but it's out of my knowledge scope.

I have these constructors:

cObj::cObj()
{
}

cObj::cObj(std::string filename)
{
    //...
}

So, basically I want to call following method, create a pointer to the object, and populate it inside of my switch-case:

void someThing() {
    cObj myObj();

    switch (someValue)
        case 0:
            myObj("/some/path");
            break;
        ...
}

I assume my constructor is wrong since it does not really work.

How can I accomplish this?

2 Answers 2

4

You don't need a pointer for this.

void someThing() {
    cObj myObj; // Don't use parentheses for the default constructor.
                // What you had was a function declaration, not an object creation.

    switch (someValue) {
        case 0:
            myObj = cObj("/some/path");
            break;
        ...
}

If you didn't have a default constructor, or you didn't want it to be called, then you could use a pointer, preferably smart:

void someThing() {
    std::unique_ptr<cObj> myObj;

    switch (someValue) {
        case 0:
            myObj.reset(new cObj("/some/path"));
            break;
        ...
}

Or, as lmmilewski hinted at, you could factor out the decision to a function, and return the object:

cObj choose(someType someValue) {
    switch (someValue) {
        case 0:
            return cObj("/some/path");
        ...
}

void someThing() {
    cObj myObj(choose(someValue));
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Its as if when i try to use the object in the first method you mentioned its empty? The constructor is called, but when i use the object its empty..
@JavaCake: It's default constructed. Then in the switch statement, I assign it a new value. (assuming you haven't done something to disable the assignment operator)
3

When you create your object in this line

cObj myObj();

(btw. you probably don't want these parentheses. You want to create an object, not declare a function).

you call the constructor. You can not call it again in switch statement. You could create a separate method:

cObj::cObj()
{
}

void cObj::SetFilename(const std::string& filename) {
// ...
}

and use it like that:

void someThing() {
    cObj myObj;

    switch (someValue)
        case 0:
            myObj.SetFilename("/some/path");
            break;
        ...
}

I'm not sure what you're trying to do, but maybe better way would be to first determine what the filepath is and then create the object?

void someThing() {
    std::string filepath = "default/path";

    switch (someValue)
        case 0:
            filepath = "some/path";
            break;
        ...

    cObj myObj(flepath);
}

You could also create a function that would make the decision what path to use and return the object:

3 Comments

cObj myObj(); doesn't create the object, because of most vexing parse — it's actually a function declaration.
+1 for your last idea (factoring out the decision to a function), which is what I normally do in situations like this.
The last method is very clean i like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.