LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 01-20-2007, 10:51 AM   #1
Winter Knight
Member
 
Registered: Nov 2005
Distribution: Debian Stable/Testing
Posts: 54

Rep: Reputation: 15
Question c++ question, how to define a member array, and it's size, outside of the class dec..


My code looked like this. Not exactly like this, but this is a shorter example:

---------------
#include <iostream>

#define NUM_NAMES 2

class A
{
public:

static char * names[NUM_NAMES];
};

char * A::names[NUM_NAMES] = { "Bob", "Sue" };


int main(int argc, char ** argv)
{
for (int i = 0; i < NUM_NAMES; i++)
{
std::cout << A::names[i] << std::endl;
}

return 0;
}

-----------------------

It works fine, but if I want to add another name, I have to do that, and I have to change NUM_NAMES. If these become out of sync, I could get into trouble.

So, I changed it to this:

--------------------------
#include <iostream>

class A
{
public:

static char * names[];
};

char * A::names[] = { "Bob", "Sue" };
int NUM_NAMES = sizeof(A::names) / sizeof(char *);

int main(int argc, char ** argv)
{
for (int i = 0; i < NUM_NAMES; i++)
{
std::cout << A::names[i] << std::endl;
}

return 0;
}

------------------------------
I thought this was more c++ like. Doing it this way, to add another name to the list, you just do that. NUM_NAMES takes care of itself.

Unfortunately, it segfaults. At least, the real program does. It only segfaults at the very end, but I've traced it back to here. The memory does not get set aside like I hoped it would.

How can I have static member variable array, and be able to define the array, and it's size, outside of the class declaration?

I could just put Bob and Mary and everyone else, inside the class declaration, but the real version is very long, and that would be ugly and not very c++ like. Also, I keep the declaration and the implementations in different files, making it more inconvenient to edit both NUM_NAMES and the names themselves.

Thanks in advance.

Last edited by Winter Knight; 01-20-2007 at 10:53 AM.
 
Old 01-20-2007, 12:21 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The C++ way would be to use a vector.

The problem with the approach that you have used is that (as you found) the memory isn't allocated, so names may point to an area of memory but there is not any memory allocated to hold your data. You will need to allocate the memory using new, or as I said use the vector template and it will manage the memory for you.
 
Old 01-23-2007, 08:28 AM   #3
Winter Knight
Member
 
Registered: Nov 2005
Distribution: Debian Stable/Testing
Posts: 54

Original Poster
Rep: Reputation: 15
Smile

Thank you. Your response was short and 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
Define a tokenizer class spank Programming 1 10-19-2006 12:10 PM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 12:35 PM
copy constructor for class containing array of vaiable size objects ? qwijibow Programming 4 12-21-2005 10:50 PM
php projects: define each class in a seperate file? ldp Programming 4 09-16-2004 06:00 PM
dynamic array in class help PTBmilo Programming 6 03-09-2003 03:35 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration