The reason behind strict rule of passing an array by pointer to a function in C++ standard.

G

Good Guy

In C++ the only way to pass an array to a function is by pointer,
considering following
functions:

void someFunc(sample input[7]){
//whatever
}
void someFunc(sample input[]){
//whatever
}
void someFunc(sample (& input)[7]){
//whatever
}

All above function parameters are identical with following function
parameter when the function is not inlined:

void someFunc(sample * input){
//whatever
}

Now to pass the array with value we have to put it in a structure like
below:

struct SampleArray{
public:
sample sampleArray[7];
};

Now I'd like to know if anyone knows the reason behind this design in C
++ standard that makes passing pure arrays by value impossible by any
syntax and forces to use structs.
 
A

Andrea Crotti

Good Guy said:
In C++ the only way to pass an array to a function is by pointer,
considering following
functions:

void someFunc(sample input[7]){
//whatever
}
void someFunc(sample input[]){
//whatever
}
void someFunc(sample (& input)[7]){
//whatever
}

All above function parameters are identical with following function
parameter when the function is not inlined:

void someFunc(sample * input){
//whatever
}

Now to pass the array with value we have to put it in a structure like
below:

struct SampleArray{
public:
sample sampleArray[7];
};

Now I'd like to know if anyone knows the reason behind this design in C
++ standard that makes passing pure arrays by value impossible by any
syntax and forces to use structs.

In C++ you can pass by pointer AND by reference.
void func(const Type& obj)

In general it doesn't make sense to pass by value things that are not
basic types, since it involves a whole copy of the object in the stack
of the function.

And by the way I've never seen this thing before
struct SampleArray{
public:

sure it's legal?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top