constructor as function argument

N

Nikola

Hi,

I don't know what does C++ standard say about this (I've googled it, but
found nothing), but in VS6 I managed to compile something like this:

function1:):class1(), arg2fun);

, i.e., for function with this definition:

sometype function1(class1 arg1fun, sometype arg2fun);

, I can instantiate constructor of the class to be used as the argument of
the function. However, this does not compile with gcc :(

Any suggestions on how this should be done? The idea is to avoid declaring
an object of class1 for a mere purpose of passing it to function1.

Thanks,

Nikola
 
E

E. Robert Tisdale

Nikola said:
I don't know what does C++ standard say about this

(I've googled it, but found nothing),
but in VS6 I managed to compile something like this:

function1:):class1(), arg2fun);

i.e., for function with this definition:

sometype function1(class1 arg1fun, sometype arg2fun);

I can instantiate constructor of the class to be used as the argument of
the function. However, this does not compile with gcc :(
cat main.cc
class class1 {
private:
// representation
int I;
public:
// constructors
explicit
class1(int i = 0): I(i) { }
};

typedef int sometype;

sometype function1(class1 arg1fun, sometype arg2fun);

int main(int argc, char* argv[]) {

sometype arg2fun = 2;

function1:):class1(), arg2fun);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
/tmp/ccetY4Oa.o(.text+0x44): In function `main':
: undefined reference to `function1(class1, int)'
collect2: ld returned 1 exit status
g++ --version
g++ (GCC) 3.4.1


It compiles just fine for me.
It would link too if I had a definition for

function1(class1, sometype);
 
A

Alf P. Steinbach

* Nikola:
I don't know what does C++ standard say about this (I've googled it, but
found nothing), but in VS6 I managed to compile something like this:

function1:):class1(), arg2fun);

, i.e., for function with this definition:

sometype function1(class1 arg1fun, sometype arg2fun);

I can instantiate constructor of the class to be used as the argument of
the function.

No, you're instantiating an object of 'class1', which is in the
global namespace.

However, this does not compile with gcc :(

It should.
 
R

Rolf Magnus

Nikola said:
Hi,

I don't know what does C++ standard say about this (I've googled it, but
found nothing), but in VS6 I managed to compile something like this:

function1:):class1(), arg2fun);

, i.e., for function with this definition:

sometype function1(class1 arg1fun, sometype arg2fun);

, I can instantiate constructor of the class to be used as the argument of
the function.

The above will create a default constructed temporary object of type class1
and pass that to function1 (by copy).
However, this does not compile with gcc :(

And it didn't give you any information on why it rejected your code?
Any suggestions on how this should be done?

Not without more information about your problems with it as well as more of
the code that was rejected by the compiler. From what you told us, it
should work.
 
N

Nikola

Sorry, I made a mistake here - the function has the following declaration:

sometype function1(class1& arg1fun, sometype arg2fun);

So, it's a pass by reference.

In that case, for the modified Robert's example I got:

$ cat main.cc
class class1 {
private:
int I;
public:
explicit class1(int i = 0): I(i) { };
};

typedef int sometype;

sometype function1(class1& arg1fun, sometype arg2fun)
{
return 0;
}

int main(int argc, char* argv[])
{
sometype arg2fun = 2;

function1:):class1(), arg2fun);
return 0;
}

$ g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)':
main.cc:19: error: invalid initialization of non-const reference of type '
class1&' from a temporary of type 'class1'
main.cc:11: error: in passing argument 1 of `sometype function1(class1&,
int)'

$ g++ --version
g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

So I guess gcc does not support passing by reference of the temporary
object. At least, not without some additional compiler switches?

Also, I apologize here for the wrong terminology (I wanted to "instantiate
a constructor" :p), but I'm obviously still struggling with C++ concepts.

Anyway, thanks for help - it was valuable.

Nikola
 
R

Ron Natalie

Nikola said:
So I guess gcc does not support passing by reference of the temporary
object. At least, not without some additional compiler switches?o

C++ doesn't support that. Rvalues can only be bounde to const references.
GCC is just obeying the rules.
 
M

msalters

Nikola said:
Sorry, I made a mistake here - the function has the following declaration:

sometype function1(class1& arg1fun, sometype arg2fun);

So, it's a pass by reference.

That was a "bug" in VC6, fixed in VC7. You must create an object here,
and that can't be a temporary, because the output of function1 should
not be lost.

Regards,
Michiel Salters
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top