Skip to main content
gamedev.net
🔒 Locked

Calling a constructor directly

Started by wild_pointer 27 replies 600+ views
Original Post
wild_pointer
wild_pointer
Is there some hackery that will allow me to call a constructor without actually creating an object? For those wondering why the hell I would want to do that...

class texture
{
   ...
   
   struct bind
   {
      bind( texture *t )
      {
         // enable texturing
         // bind
      }
      ~bind( void )
      {
         // disable texturing
         // unbind
      }
   };
   
   ...
};

      
Then i was hoping to be able to use it like

void foo( void )
{
   texture::bind( bar ); // texture bound, texturing enabled

   // do a bunch of stuff where after i will inevitably forget
   // to disable textures

}  // bind destructor called

      
I realize I could just create a temp object, but I dun wanna. Alternatives welcome.
[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people [edited by - wild_pointer on April 13, 2003 4:45:11 AM]
[size=2]
mcfly
mcfly
Yes. You can overload the new operator, like this:


      
enum PLACEMENT_NEW
{
USE_PLACEMENT_NEW
};


void* operator new (size_t,void* ptr,PLACEMENT_NEW)
{
return ptr;
}


#define CONSTRUCT(ptr) new ((void*)(ptr),USE_PLACEMENT_NEW)



..and then:


              
new ((void*)bar,USE_PLACEMENT_NEW) Texture();


..or


CONSTRUCT(bar) Texture();






[edited by - mcfly on April 13, 2003 5:09:41 AM]
andy_fish
andy_fish
I think you -can- just call it directly. The right syntax would be:

texture::bind::bind( bar ); // texture bound, texturing enabled

so try that and see if it works

edit: except.. I think 'bind' must be a class and not a struct for this to work

[edited by - andy_fish on April 19, 2003 7:54:51 PM]
Zipster
Zipster
I've heard otherwise, that you couldn't call the constructor yourself. I've never tried it though, and never will. I don't think it's a good thing, you should really have a seperate function called something like Initialize if you must, constructors are for construction.

[edited by - Zipster on April 19, 2003 7:54:27 PM]
davepermen
davepermen
if your object does not occupy any space, it will not cost you anything. allocating on the stack is just a pointer increment/decrement. and then the calls to ctor and dtor of the object. you just want the ctor and dtor call, but no pointer increment/decrement. that will not happen if you don''t have anything in the object stored. compilers are smart enough. but even if they wouldn''t, it wouldn''t hurt you at all really.


so

{
bind ___temp_var___(someparams);
}

would work exactly as you want.

now, you don''t want to write the name as well.. so just generate one..

something like this:

#define LOCALE LOCALE_VARIABLE____##__LINE__

or similar should work..

so
{
bind LOCALE (someparams);
}

possibly you could call LOCALE auto instead (an anyways useless c++ keyword), and then you get similar-to-d-behaviour. a scoped object.


{
bind auto(someparams);
}


should work. but haven''t tried.

don''t care about the temp object. it will never exist.

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
andy_fish
andy_fish
I was curious, so I just ran some test code..

You CAN call a constructor like a normal function.

Calling texture::bind::bind(bar) should work fine.

(My other comment saying that ''bind'' needs to be a class was wrong, it still works if ''bind'' is a struct)
Cedric
Cedric
If you don''t want to create a temporary, make a public initialize function and have the constructor call it too.

Cédric
andy_fish
andy_fish
What is the point of all this silliness? Why worry about temporary objects or ctor or dtor?

You can call a constructor like a normal function! Just try it!
antareus
antareus
So are you certain that creating the object is the slowdown? Or are you just talking like you think it would? If you specify the texture object as inline the release version you should see zero performance hit from even making the object.

Senseless, premature optimization. Resource acquisition is initialization is an important idiom for C++.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
daerid
daerid
quote:
Original post by andy_fish
What is the point of all this silliness? Why worry about temporary objects or ctor or dtor?

You can call a constructor like a normal function! Just try it!


I''d like to know exactly what compiler you''re using, as calling a constructor directly without creating an object is, last time I checked, nowhere NEAR being standard C++. Nor can constructors be static.

So I''d say either a) You have a crappy compiler, b) You''re on crack, c) I''m on crack

:shrug:
daerid@gmail.com
Zipster
Zipster
Sometimes you really do want an object to be created.

I remember something like this being discussed before, for example inside the constructor all the current settings would be saved, and when the destructor was called, they would be restored. This was especially useful (for me at least) when I was working in OpenGL and I wanted to change some settings temporarily. By having the object created, not only could the settings be saved somewhere, but I could also nest these objects, so that at different levels different settings would be saved and then restored on deconstruction.

In your case though, it doesn't make any sense to do this in a constructor. If you don't have an instance, then the destructor wouldn't be called, and it appears as though you have no need for an object anyway. Might as well make this a static function, unless the bind structure has some other important use.

[edited by - Zipster on April 20, 2003 4:24:53 AM]
SabreMan
SabreMan
quote:
Original post by andy_fish
You CAN call a constructor like a normal function.

No you can''t.
cow_in_the_well
cow_in_the_well
I tried calling the constructor of a class manually (by doing CFoo::CFoo ()a while ago, and it had some very evil results. It didn''t actually crash the program, but when i stepped into the constructor where i called it the this pointer turned into something else entirely. I have no idea where it got the new this pointer from but i''m not gonna try doing that again .

- Thomas
SabreMan
SabreMan
quote:
Original post by cow_in_the_well
I tried calling the constructor of a class manually (by doing CFoo::CFoo ()a while ago, and it had some very evil results. It didn''t actually crash the program, but when i stepped into the constructor where i called it the this pointer turned into something else entirely. I have no idea where it got the new this pointer from but i''m not gonna try doing that again .


It probably created a temporary object.
wild_pointer
wild_pointer
You can explicitly call the constructor within the object itself; I assume the compiler creates a temp object for you (In VC6 anyways). This of course does not work outside the object.


class foo
{
foo();

void bar()
{
foo();
}
};


Thank's again for the input. davepermen, thanks in particular, for the ideas.

I guess my question was poorly thought out, I should have said is there a way to call the constructor without explicitly creating an object... Of course I would need one if I wanted to take advantage of the destructor being called upon leaving scope.




[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on April 20, 2003 7:22:59 PM]
[size=2]
daerid
daerid
quote:
Original post by wild_pointer
You can explicitly call the constructor within the object itself; I assume the compiler creates a temp object for you (In VC6 anyways). This of course does not work outside the object.

[code...]

Thank''s again for the input. davepermen, thanks in particular, for the ideas.

I guess my question was poorly thought out, I should have said is there a way to call the constructor without explicitly creating an object... Of course I would need one if I wanted to take advantage of the destructor being called upon leaving scope.


First off, that isn''t calling anything, it''s declaring a constructor. Secondly, I''d like to know exactly what your problem with creating an object is. A class with no members is only sizeof(char) in size. You''re telling me that you are worried about giving up a single byte (on virtually every compiler) on the stack, only for the duration of the calling function? You working on embedded code or something?
daerid@gmail.com
wild_pointer
wild_pointer
quote:
Original post by daerid
First off, that isn't calling anything, it's declaring a constructor. Secondly, I'd like to know exactly what your problem with creating an object is. A class with no members is only sizeof(char) in size. You're telling me that you are worried about giving up a single byte (on virtually every compiler) on the stack, only for the duration of the calling function? You working on embedded code or something?


I don't think you fully read my code example. You can see the constructor is being called in my pseudo function "bar". I'm sufficiently aware of the difference between calling and declaring methods.

I understood the performance/memory hit was minimal; it wasn't really my primary concern. I was simply wondering if there was a way to acomplish the same thing without (explicitly) creating a temporary object which seemed unintuitive at best.





[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on April 20, 2003 10:56:19 PM]
[size=2]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.