1

So far I've read about structs, with and without pointers. My question is about a struct in combination with a class and main. What I learn from K&R and c++ sites is that a struct can't contain values. The values I want to assign to the members will be constants. In several posts I read that I should put the struct inside the class and even inside private. The setup I use was:

class C
{
struct something {const float m = 3} s;` //actually 12 members. Compiler doesn't accept  values: "ISO C++ forbids initialization of member"

function foo(struct something s){ float m = s.m; <..do something..>}` //struct s actually used in several functions

};    

int main(){C c;}

Then I created 2 structs, and letting the 2nd assign values to the members of the first, which I find ugly. But why did that get accepted by gcc? So how can I assign the values only once in a proper way since assigning values has to be done inside a function. BTW, I'm using Ubuntu gcc-4.6.1.

Thank you for an answer.

Caesar
  • 9,483
  • 8
  • 40
  • 66
HvNiekerk
  • 33
  • 2
  • 10
  • 3
    You are confusing languages. In C++, `class` and `struct` are essentially the same. You can express exactly the same types using either. – juanchopanza Oct 23 '13 at 08:27
  • structs can't contain values? – Kakalokia Oct 23 '13 at 08:33
  • Of course structs can contain values. And you shouldn't use K&R to learn C++, since it's about a different language. You might find a [C++ book](http://stackoverflow.com/questions/388242) more useful. – Mike Seymour Oct 23 '13 at 08:38

3 Answers3

1

The difference between struct and class in C++ is that structs default their members to public while classes default to private (same goes for inheritance). That's all there is to it.

Then, in C++03 const float m = 3; is not valid as a member declaration. You would need to declare a constructor for your struct:

struct something
{
    const float m;
    const float n;
    something() : m(3), n(42) {}
} s;
nikolas
  • 8,707
  • 9
  • 50
  • 70
  • How do you do this when there are more constants? Now I get "something() cannot be overloaded". – HvNiekerk Oct 23 '13 at 13:53
  • @user2294285 updated my snippet for two constants, more are analogue – nikolas Oct 23 '13 at 14:05
  • Thank you. I've searched for this in Stroustrup 3rd ed., but as far as I can see the examples don't show this. Where can I find more of this ? – HvNiekerk Oct 23 '13 at 14:36
  • @user2294285 That concept is called an [initialization list](http://stackoverflow.com/questions/4589237/c-initialization-lists) (not to be confused with the C++11 feature `std::initializer_list`) and you should be able to find something on that; but I don't have access to the book so I can't say for sure – nikolas Oct 23 '13 at 14:41
1
struct something {const float m = 3} s;` //actually 12 members. Compiler doesn't accept  values: "ISO C++ forbids initialization of member"

to fix above do

struct something {
    something (float const& f = 3) : m(f)
    const float m;
} s;
Vishnu Kanwar
  • 761
  • 5
  • 22
0

Structs AND classes in C++ suffer from this problem. You can only declare a value in a class definition if the variable is static and shared among all the instances of the class. If your variable is const I think that may be a good choice.

A good solution for non-const variables is a simple one - assign the desired values to all the variables in the default constructor of your class or struct.

e.g

struct Something
{
  float value;

  Something():value(5.0f) {};

}
Lloyd Crawley
  • 606
  • 3
  • 13
  • That doesn't apply anymore. – juanchopanza Oct 23 '13 at 09:24
  • @juanchopanza What doesn`t apply anymore? I made two points please be specific as to which point you are referring, and also please state which standard changed the rules. – Lloyd Crawley Oct 24 '13 at 01:23
  • 1
    Your first point. You can now say `struct Foo { int i = 42; };`. `i` doesn't have to be `const static`. – juanchopanza Oct 24 '13 at 05:04
  • That`s great to know! Thank you. Is this something that came with c++11 or did an earlier standard change that? Have I been sleeping for years? – Lloyd Crawley Oct 24 '13 at 06:57
  • It is new in C++11. You've only been sleeping for two years :-) To be fair, this feature took a while to implement in the most popular compilers. – juanchopanza Oct 24 '13 at 06:59
  • Thanks for the heads up. I really should learn some C++ 11 things, haven`t had the chance yet. However, the thought of C++ with even MORE features makes it that much more intimidating. – Lloyd Crawley Oct 24 '13 at 08:44