Need help with struct and class

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.
A couple of comments on your example:

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.

Last edited on
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Function
{
        struct XYZ
	{
		std::string Name;
		double Value;
	};

        XYZ xyz;
  public:
         void Func()
         {
               xyz.Name;
               xyz.Value;
         }
	};



Now is this code correct? I just want to know how to access it in the methods of the class.
Now is this code correct?
Yes, again: Appart from the scope there is no difference.

Second I don't need to define the default constructor or the destructor as the compiler does that for me automatically.
While this is true Value will not be initialize because the default contructor doesn't do anything in case of a POD.

https://en.wikipedia.org/wiki/Passive_data_structure
http://www.cplusplus.com/reference/type_traits/is_pod/
Lines 13-14 are not valid useless 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.
Last edited on
I get the constructor part but if there is error on line 13 and 14 and there surely is then what is the correction for it?
What do you think your line 13 and 14 do?

They should be:
13
14
  xyz.Name = "";  // not really required since std::string will default to an empty string
  xyz.Value = 0.0;

Note the assignments to the members of xyz.
Last edited on
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.
Sorry, I mistakenly thought Func() was Function's constructor, which is why I assumed you wanted to initialize them there.

You certainly should be able to reference xyz's members within Func().

I compiled your snippet, with only the addition of the #include and received no compiler errors. Please post full text of the errors you received.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>

class Function
{      struct XYZ
	{
		std::string Name;
		double Value;
	};

        XYZ xyz;
  public:
         void Func()
         {    xyz.Name;
               xyz.Value;
         }
};


Note that in the above snippet lines 13-14 don;t do anything and xvz.Value is never initialized.
I was trying this with a template class. Something like this


1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>class Function
{      struct XYZ
	{
		T Var;
	};

        XYZ xyz;
  public:
         void Func()
         {    
                xyz.Var;
         }
};


I am using Visual Studios and when I pressed the '.' after xyz it showed nothing.
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.
Last edited on
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?
Hi,

As AbstractionAnon is saying, line 11 is useless - it doesn't do anything. Maybe you want to assign some value to it?

You should have a constructor, the whole point of these is to initialise the values of the member variables.

In any case, nothing happens until you make an object of that class type. That is, you need to make an instance of the class.

Function is not a good name for a class. I guess that's pretty obvious, but I will mention it anyway.


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

1
2
3
4
void Func(const T& data)
         {    
              xyz.Var = data;
         }


Something like that.
Topic archived. No new replies allowed.