LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-08-2011, 07:51 PM   #1
wagaboy
Member
 
Registered: Jun 2010
Distribution: Ubuntu 10.04, Cent OS 5.5, CLE3
Posts: 51

Rep: Reputation: 21
C++ class inside struct


Is is possible to use a class inside a struct ? I keep getting segmentation fault with this code:

Code:
struct my_struct {
    unsigned count;
    std::string msg;
};

int main () {
    ...
   my_struct* temp = (my_struct*) malloc(sizeof(my_struct));
   cin >> temp->count; // Works fine
   cin >> temp->msg; // Segmentation fault :(
    ...
}
 
Old 04-08-2011, 09:47 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
Q: Is is possible to use a class inside a struct?
Sure. Just don't "malloc()" a class

Declare it in-line, or use "new".
 
1 members found this post helpful.
Old 04-08-2011, 11:58 PM   #3
wagaboy
Member
 
Registered: Jun 2010
Distribution: Ubuntu 10.04, Cent OS 5.5, CLE3
Posts: 51

Original Poster
Rep: Reputation: 21
paulsm4, thank you. I thought I could use malloc since it was a struct, but as you said struct is also a class.
 
Old 04-09-2011, 07:39 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by wagaboy View Post
I thought I could use malloc since it was a struct, but as you said struct is also a class.
You could use malloc correctly on a class or struct if that class or struct did not need its constructor to be called. That doesn't mean you should use malloc in that case. I'd still use new.

In your example, what matters is the fact that std::string has a default construct and a std:string requires initialization by one of its constructors.

Your struct has no source code for constructors, but every member either has a default constructor or doesn't have any constructor, and at least one member has a default constructor. That all causes the compiler to provide default constructor for your struct. Using new causes the constructor to be used. Using malloc bypasses the constructor.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
struct inside a struct primenu Programming 4 11-25-2009 03:23 PM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
struct in class AquamaN Programming 3 06-29-2004 03:25 PM
Push into Vectors inside struct in C++ !! Hady Programming 6 05-14-2004 08:34 AM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

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

All times are GMT -5. The time now is 05:32 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration