Solved

static initialization of array of objects

Posted on 2002-05-03
6
179 Views
Last Modified: 2010-04-02
I have 2 questions

1. Just a thought,  Does it make sense in having ch -- an array of one char.

     char ch[1];


2. static initialization of array of objects
   
#include <iostream.h>
class ABC {
 int i;
public:
 ABC(i)
 {
  cout << i << endl;
 }
};  // end of class ABC
 

void main()
{
     ABC a[5] = {1,2,3,4,5};
   // whats the problem in static initialization of
   //array of objects like this.
}
0
Comment
Question by:rv_san
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6988454
It will work if you make it read

ABC a[5] = { ABC(1), ABC(2), ABC(3), ABC(4), ABC(5)};

0
 
LVL 86

Expert Comment

by:jkr
ID: 6988458
>>Does it make sense in having ch -- an array of one char.
>>    char ch[1];

That depends on what you want to do with it...
0
 

Author Comment

by:rv_san
ID: 6990812
ABC a[5] = {ABC(1),ABC(2),ABC(3),ABC(4),ABC(5)};

works fine. Thanks


Secondly, regarding char ch[1], I was wondering if I use "char ch[1]" as an array then the array space will be used only to store '\0'  Is there any practical situation where I must have to use "char ch[1]"  only.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:mirtol
ID: 7012600
Nope.

The only use for defining an array of 1 unit is if you want the compiler to treat the variable as an array when you don't actually know how big it will be.

For example:

struct somepacket {
    int     somevar;
    ...
    int     datalength;
    char    data[1];
}

This way, you can reference the data in you code as an array very easily.

In any other case, you can always reference the single char variable.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7178496
Dear rv_san

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jkr"

comment(s) as an answer.

If you think your question was not answered at all, you can explain here why you want to do this and post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7199741
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now