LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2009, 07:51 AM   #1
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Why is this syntax legal in C, but not C++?


On occasion I run across code similar to the following:
Code:
struct Foo
{
  int value1, value2, value3;
};

int main()
{
  struct Foo f = { .value1 = 10, .value3 = 20 };
}
This type of structure initialization is helpful when only certain field(s) of a structure need to be initialized, where other field(s) can be ignored.

Typically, one normally sees an initialization as follows:
Code:
...
  struct Foo f = { 10, 0, 20 };
...
Anyhow, why is the first approach supported in C, but with C++, the compiler spits out syntax errors?
 
Old 03-13-2009, 08:18 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I had no idea you could do that in C.

It sure would be a great feature in C++.

In more complicated cases, it can be much more important to initialize members (or map contents, etc.) by name.

I usually do this (quoted from memory, not retested, so I might have some detail wrong):

Code:
int main()
{
  struct f_initialized : public Foo {
     f_initialized() {
       Foo &x = *this;
       x.value1 = 10;
       x.value3 = 20; }
  } f;
}
Obviously, that is a lot of overhead just for initializing a couple members. But in many cases (such as class static variables) you really want the initialization done as part of construction of the object. Sometimes the limited syntax of the existing constructor or initialization list is too restrictive.

If the above code isn't obvious: It defines a class derived from Foo for the purposes of initializing this one object. Inside the class definition, it defines a constructor which does the desired initialization by member name. Then it defines f as a single instance of that class.

In some cases, there may be consequences from the fact that the type of f is f_initialized rather than Foo. But in most cases where I have used this idiom, that detail doesn't matter.
 
Old 03-13-2009, 11:58 AM   #3
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Because C and C++ are diffrent languages. While people claim that C is a subset of C++, the fact is that this is wrong. Most commonly used C features and syntax are supported in C++, but not all, and it just so happens that you found a feature that C has that C++ does not have.
 
Old 03-13-2009, 06:49 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In C++ a struct is a class with all its members public by default. Because it is a class the rules are different from C which doesn't have a concept of classes. Use constructors to initialise the values.

Whilst you can use an initialiser list if you have no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions. In which case the members will be initialised in order and if less values are in the initialiser list than there are members then the latter ones will be default initialised.

Last edited by graemef; 03-13-2009 at 07:42 PM. Reason: adding initialiser list
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is This Legal?? miltonsj Ubuntu 7 01-02-2007 10:29 PM
Legal Help acratus General 9 01-26-2006 07:03 AM
Ya think this is legal??? fenderman11111 General 8 02-04-2005 05:48 PM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM
Is it legal?!? shusseina Linux - Distributions 5 10-27-2002 02:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration