2

please see the snippet of my c++ code below. Because foo.h is executed before int main(int argc, char *argv[]), the array RedApple will be initialized with size 0 and causes an error. What is the best way to deal with this problem? Is there a way to keep the class declaration in foo.h, but initialize it in foo.cpp from the user input? Thanks!

In foo.h

#include <vector>
extern int num;
class apple
{
std::vector<long> RedApple;
public:
    apple(): RedApple(num)
}

In foo.cpp

#include    "foo.h"
int num;
int main(int argc, char *argv[])
{
sscanf_s(argv[1],"%d",&num);
}
3
  • 3
    Give the constructor a parameter. Use it to initialize the vector. Commented Nov 25, 2014 at 20:28
  • Could you please elaborate a little bit? Commented Nov 25, 2014 at 20:31
  • Can you give us an idea what is your intention? Your class definition is never used in your code snipped. And please get an idea how global objects/data are initialized! There is nothing in c++ which makes the sentence "foo.h is executed before " right. The startup code of your application initializes all global data before entering main. But the order of initialization is more or less undefined and depends on your compiler and linker settings and order of files during linkage. Init a vector with a number creates a vector of the given size and not with the value inside! Commented Nov 25, 2014 at 20:37

1 Answer 1

1

In foo.h

#include <vector>

class apple
{
std::vector<long> RedApple;
public:
    apple(){}
    apple(int num): RedApple(num){}
}

In foo.cpp

#include    "foo.h"

int main(int argc, char *argv[])
{
    int num;
    sscanf_s(argv[1],"%d",&num);

    apple foo = num > 0 ? apple(num) : apple();
}

EDIT: In response to Klaus' complaint I thought I'd add an explanation of the initialization, I'm commenting the line apple foo = num > 0 ? apple(num) : apple(); so I'll break it vertically commenting on each word:

apple      // This is the type of variable in the same way int is the type of int num;
foo        // The name of the apple object that I am creating
=          // Normally this will trigger the assignment operator (but in a declaration line the compiler will optimize it out)
num > 0 ?  // The condition to a ternary operator if true the statement before the : is executed if false the one after is executed
apple(num) // If num is greater than 0 use the non-default constructor
: apple(); // If num is less than or equal to 0 use the default number cause we can't initialize arrays negatively
Sign up to request clarification or add additional context in comments.

10 Comments

your line apple foo = num == 0 ? apple() : apple(num); looks very dirty for me. Here a beginner is asking and you provide a nearly unreadable code snipped. And I think it is not a good idea to init a large vector and assign it to a already given instance of another instance which can result in a huge copy operation.
@Klaus I did the "nearly unreadable code snipped" so that I wouldn't have to copy. Depending on your compiler that may translate to a move, but usually the compiler will just treat that as though I'd just called that ctor directly.
You are right: It may be optimized out. But the code can be written much simpler and you can avoid any copy without the hope of optimizing. And why you should decide between apple() and apple(num)? The default constructor also results in an empty vector which is the same as apple(0). So your code potentially produces a bad/slow executable without any need or benefit.
Thanks for your answer @JonathanMee. If I have 3 vectors RedApple(num1), GreenApple(num2), BlueApple(num3), how should I pass num1, num2 and num3? Is it like apple(int num1, int num2, int num3): RedApple(num1), GreenApple(num2), BlueApple(num3){} ? Many thanks!
@goosli Yeah, rather than having a constructor that just takes 1 variable you'd want one that took 3. So it would look like: apple(int num1, int num2, int num3) : RedApple(num1), GreenApple(num2), BlueApple(num3) {} You'd also need to check all three in your call: apple foo = num1 >= 0 && num2 >= 0 && num3 >= 0 ? apple(num1, num2, num3) : apple();
|

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.