Casting in functions params

This is a discussion on Casting in functions params within the C++ Programming forums, part of the General Programming Boards category; I'm trying to do something that I remember having seen somewhere, though I can't remember where I saw that... I ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Casting in functions params

    I'm trying to do something that I remember having seen somewhere, though I can't remember where I saw that... I try to pass in a function an object that's not yet defined, I don't know how to explain it, but I believe it has something to do with constant casts...

    Code:
    struct foo
    {
    	int a, b, c;
    	foo(int d, int e, int f)
    	{
    		a = d; b = e; c = f;
    	}
    };
    
    void func(foo obj)
    {
    	//Something with the "obj"...	
    }
    
    //Under main:
    func( (foo){1, 2, 3} );
    
    //Or something that'd give the same result:
    foo OBJ(1, 2, 3);
    func( OBJ );
    I saw something like that somewhere, I know that it convert the "array" to assign it to the variables defined in the class that is kind of "casted"... As if a "foo" object was defined directly in the sent params, as you would simply do with any number constant...

    How can I achieve to make "(foo){1, 2, 3}" works? Is it a cast, const cast, or an overloading of constructor or whatever? Because I think it may not be an overload, if you don't initially have an instance var of that structure... Or there is some concept I am mixing up... Casting "(foo)" doesn't create an instance of that structure, right?

    Thanks in advance.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    What you want is to do:

    func( foo(1,2,3));

    which does create a temporary (unnamed) foo object, and passes that to the function.

    It's not a cast. It's calling the constructor as normal, and making an object.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Thumbs up

    Haaaaa.... It was just this, I was sooo close.... Thanks Cat!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 05:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21