Hi All,

I below code snippet I have been using diff-2 structure with same variable due to having different -2 default values
for the variable (var) in current implementation for both the structure a and b. This way i need to create 17 structure with same variable (var) but different default values.
Could you let me know what is the best way to achieve the same with having same sturcture with different-2 values.
Apart from this it is very much required to initialize the varaible (var) with default value in structure as it shouldn't be uninitialized.

    #include <iostream>
    using namespace std;

    struct a
    {
        int var;

        a()
        {

         var=0;   
        }
    };

    struct b
    { int var;
    b()
    {  
    var=12;

    }
    };
    void test(a& obj);
    void test( b& obj1);
    int main() {
          a obj;
          b obj1;

        test(obj);
        test(obj1);
        //test(a, b);

        return 0;
    }

    void test( a& obj) 
    {int avar;
        avar=obj.var;
        cout<<avar;

    }

    void test( b& obj1)
    {int bvar;
       bvar=obj1.var; 
       cout<<bvar;  
    }

Just a comment about code formatting. Your code above is all over the map. Extra blank lines, varying parens and brace alignment and so on. It's not easy to read so it made it hard to follow. When presenting code to others, pick a coding style and stick to it.

Examples at https://en.wikipedia.org/wiki/Indent_style

You are never calling the member a() and b() functions to initialize the variables, so they contain random data.

There is no issue with random data here, I am getting proper values 0 and 12.

@OP, rubberman is right. You are falling for the trap "it works." Uninitialized variables is a big no-no in programming. Just because it works does not mean it's correct. I'd stop and learn this before you get burned later over it.

Here's the google on that. https://www.google.com/search?q=c%2B%2B+uninitialized+variables&ie=utf-8&oe=utf-8

So in some IDEs the memory space is cleared for you but when you compile or run outside the IDE it can blow up.

True story. I was on a team that traveled over 6 thousand miles many times over this issue. The code had been outsourced and they didn't believe you needed to initialize variables. Hard expensive lessons for both companies. For me, a breeze of a job since it was not a design issue but one of the basics.

You are never calling the member a() and b() functions to initialize the variables, so they contain random data.

a() and b() are the default constructors. They are called when the objects are created on lines 26 and 27. var is initializized to 0 and 12 in those default constructors.

To the OP, I can't understand what you are asking. You have 17 different structures containing a member called var and you want to avoid writing 17 different constructors? Is that the problem?