Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

C++ : Simple array in class declaration...

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is a simple question, but I can't seem to find an anwer anywhere for it... If I want to use a simple, stack-based, C-style array in a class, how is it done? I'll give an example:
class foo
{
    public:
    foo();

    private:
    string bar[];
}

foo::foo()
{ }
How do I initialize bar[] in the constructor? In main(), I could: string bar[] = {"this","is","an","array"}; I've tried various forms of constructor initialization and my g++ compiler is not a happy puppy about it. Maybe There's a syntax problem with what I'm using. consider options:
foo::foo()
  :bar[]({"this","is","an","array"})    // fails
{ }

foo::foo()
  :bar[]("this","is","an","array")    // fails
{ }

foo::foo()
  :bar({"this","is","an","array"})      // fails
{ }

foo::foo()
{ bar[] = {"this","is","an","array"}; } // fails.
I've been successful at allocating a heap-based array, but not a stack-based one. Just can't seem to find an answer to this. --random

Share this post


Link to post
Share on other sites
Advertisement
Yeah. Unfortunately this cannot be done in C++ without using a hack. You might want to take a look at this thread, which is one of several on the topic on these forums.


jfl.

Share this post


Link to post
Share on other sites
Yikes! I was afraid this would be the case. Even Stroustrup's book "The C++ Programming Language" avoids this subject completely.

I've looked at the thread but it seems like 'going around your elbow to get to your nose'! It is easier to achieve the same thing by just doing a:

vector<string> bar;

but I was hoping to just use a very simple light-weight array.

I've seen some numerical code that uses a static array declared as a preface (header) to either the class declaration or method definition file. Something like:

static int _bar_[] = { 0, 1, 2, 3 };

Of course this is not the same at all as private member usage, but it can lead to speed improvements, I've noticed.

--random

Share this post


Link to post
Share on other sites
Well, static arrays will be per-class, not per-instance. If that is not a problem, then you can initialize it like a normal local array, like so:
class Foo {
static int array[];
};
int Foo::array[] = { 1, 2, 3, 4, 5 };


Otherwise, if you know what size your array will be at compile-time, then you can just declare it the right size in you class definition and fill it manually in your constructor.

I wonder if they deal with this in C++ 0x (Or C++/CLI, for that matter).


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
Well, static arrays will be per-class, not per-instance. If that is not a problem, then you can initialize it like a normal local array, like so:
class Foo {
static int array[];
};
int Foo::array[] = { 1, 2, 3, 4, 5 };


Otherwise, if you know what size your array will be at compile-time, then you can just declare it the right size in you class definition and fill it manually in your constructor.

I wonder if they deal with this in C++ 0x (Or C++/CLI, for that matter).


jfl.


That's a great idea, what I wanted to do is just set properties for each class in a simple format, but my concern was that, by doing it as a static outside the class definition, it could be accidentally accessed by some other part of my program.

However, if it can be prefaced as 'Foo:array[]={...}' then this solves that problem, and, I suppose that other classes that have a member that points to an instance of 'Foo' can access this member as well.

I noticed that objective-C actually has a way to designate whether members are per-class or per-instance. I suppost that this is the way to handle this issue in C++.

Thanks All..

--random

Share this post


Link to post
Share on other sites
Come to think of it, in objective-C this concept of 'per-class or per-instance' is quite a central concept and one would think that it should be so for any oop language, really. However, this is the first time that I've come across it in C++.

Strange!

--random

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!