473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

constructor as function argument

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
Jul 22 '05 #1
6 2247
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
Nikola wrote:
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);
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.

Jul 22 '05 #3
Nikola wrote:
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.
The idea is to avoid declaring an object of class1 for a mere purpose of
passing it to function1.


Jul 22 '05 #4
On Fri, 03 Dec 2004 16:00:49 -0800, E. Robert Tisdale wrote:
Nikola wrote:
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);


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
Jul 22 '05 #5
Nikola wrote:
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.
Jul 22 '05 #6

Nikola wrote:
On Fri, 03 Dec 2004 16:00:49 -0800, E. Robert Tisdale wrote:
Nikola wrote:
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);

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

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
17
by: mrstephengross | last post by:
I've got a 'Command' class whose constructor takes an instance of a template argument T. The constructor then prints out the name of the class T that was passed to it. When I construct the T class...
15
by: Alexander Stippler | last post by:
hi, the following does not work. I do not understand why, since it works if I replace line marked by (1) with the line below ((2)). Compiling with gcc4.0 results in "error: conversion from...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
5
by: Edward Diener | last post by:
The first type to a delegate constructor is obvious. It is a System::Object * . What is the MC++ type of the second argument to the delegate constructor ? In C++ it would be a member function...
6
by: Rufnex | last post by:
Hi, is there a delete for a object inside the constructor, while i init it? i will try something like that: var obj = function(a) { if (!a) delete this; this.a = a; }
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.