I want a complete example on the use of a struct inside a class. I want to know how to initialize its variable in the class. And then how to access this member by a function.
1 2 3 4 5 6 7 8 9 10 11
class Function
{
struct XYZ
{
String Name;
double Value;
};
public:
Function();
virtual ~Function();
};
Something like this. Thanks for your help in advance.
A nested struct/class is the same as a non nested. The only difference is the scope. I.e. you need to add the encompassing class scope (Function::) when used outside. When used inside it is not necessary.
1) XYZ is within Function's private area, so it can't be referenced outside Function.
2) Line 5: I assume this is a typo and you meant std::string.
3) XYZ is a struct declaration only. It does not allocate any storage.
The cleanest way to initialize XYZ is to provide a constructor for it. Structs can have constructors just as classes do. Name is going to be initialized by std::string's constructor to an empty string, so the only thing you really need to initialize is Value.
1 2 3 4
// After line 6
XYZ ()
{ Value = 0.0;
}
Re #3, after line 7, you probably want an instance of XYZ.
1 2
// After line 7
XYZ xyz;
When Function is instantiated, xyz will be initialized automatically because of its constructor.
First of all I meant std::string thanks. Second I don't need to define the default constructor or the destructor as the compiler does that for me automatically. Now I want to keep the XYZ struct private and only accessible to the class. Now if I initialize a variable of struct inside the class. How can I access it in its methods. Lets change the code a bit
Lines 13-14 are not validuseless syntax, but yes, you can initialize xyz there.
As I stated earlier, the preferred way to initialize XYZ is to provide a constructor for it.
What if you were to instantiate XYZ as a local in some other functiion that is a part of Function?
In that case, without a constructor for XYZ, you would have to initialize Value every place you instantiated XYZ within your function class. With a constructor, you initialize XYZ in one place and don't have to worry about initialization if you choose to instantiate it in other places.
I don't want to initialize them in the function. Consider them initialized. I want to access them in that function. The compiler is saying this is not correct.
I am using Visual Studios and when I pressed the '.' after xyz it showed nothing.
I assume that you mean you were expecting intellisense to show you the members of xyz.
That's to be expected because the compiler doesn't know the type of T yet. That only happens when the class is instantiated.
Line 11 is still useless syntax. The compiler interprets line 11 as an expression with a single term (xyz.var). The result of the expression is ignored. I don't know at this point if you meant line 11 to be a generic placeholder for some reference to xyz.Var, or you think that line does something.
Line 11 just shows that I am trying to use the variable in the function for a task. It is just showing that I am accessing it. Please can you show me the correct way to go about it?
Yes I want to initialize a value on line 11. I know you can use a constructor for it but I want to use the function Func for it. Now is this line correct. I just want to know as AbstractionAnon pointed out that the intellisense of Visual Studios did not display the members of the struct. Here is the code inside the Func below