Solved

Determining array length

Posted on 2004-08-11
3
12,436 Views
Last Modified: 2011-08-18
Actually, this question has two parts.

1. Can C++ arrays only hold primitive types such as int, float, etc? Can it hold abstract types such as:

class A {
...
};

int main() {
    A a1, a2, a3;
    A a[] = {a1, a2, a3};
    myFunc(a); // see part 2
    return 0;
}

2. Now for my real question. When passed an array as a parameter, how do I determine the length of the array? This array contains abstract types.

From the example above, suppose I have the following function

void myFunc(A *x) {
// how do I find the length of the array
}

In C, I know that you can do the following

int size = sizeof(x) / sizeof(x[0]);


Thanks a lot everyone.

P.S. I am interested only in arrays. Not vectors.
0
Comment
Question by:licz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11780607
1. If you are interested in arrays only.
One way would be to make it an array of pointers and hold the pointers to your abstract classes in the array
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 405 total points
ID: 11780633
1. The answer is yes and no.

   you can't do  "AbsType name[10];"
   since you cannot define a variable of type with pure virtual methods

   You can do      AbsType *name = new ConcreteType[10];

   
2.  You cannot find the size of an array passed as a parameter.
You cannot even do that in C; the code you gave only works for
a static array (Yes, that code works for the same situation in C++ that
it works for in C).

f(int x[]) {
   return sizeof(x) / sizeof(x[0]);
}

Will always return  sizeof(int *) / sizeof(int)
which is not the size of the array passed.

In both C and C++

if you want to know the length, then you need to pass it, i.e.

f(int x[],  int numberElementsInArray)
{
  ...
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 11794113
In your example, you're passing a static array to your function.

If you're only going to be using static arrays, and the size is variable depending where it's being called, then you can determind the size of the array if you use a template function.

Example:
template<class T>
void MyFunc(T&Src)
{
      int Size = sizeof(Src)/sizeof(Src[0]);
      cout << Size << endl;
}

int main(int argc, char* argv[])
{
      int data1[5] = {1, 2, 3, 4, 5};
      MyFunc(data1);

      int data2[10] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
      MyFunc(data2);

0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

751 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