Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mkk

Some basic questions about c++

This topic is 5180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!! If I define a variable/array, is it always possible that there is something old stuff (from the memory) in the value´s memory address? What about inside a class? And another one: How can I make object of the class that takes parameters extern? For example I have situation like this: Object Object1(100); And I would like to have that object1 linked externally but how?

Share this post


Link to post
Share on other sites
Advertisement
The class''s constructor should take care of any garbage in the memory that it occupies.

If you want an extern declaration for a variable of class type, it works the same as an extern declaration fora a variable of primitive typpe:

extern Object Object1;

Keep in mind initialization order of classes not in the same translation unit are undefined, so you can only use globals like this safely after main() has started.

Share this post


Link to post
Share on other sites
Thanks for answering SiCrane :-)

After reading your post I understood that the problem wasn´t the wrong syntax, it was because all files weren´t conscious about the class *duh* :-)

And nice to hear that constructor will keep things clean.. Only thing that still bothers me is that how is it with the arrays.. I mean, if I define array (outside the class), is it possible that there is some trash inside of it?

Oh and tried to read your last sentence couple of times but I have to admit that I´m not getting what you are saying so if just possible try to explain it more clearly..


[edited by - Mkk on April 15, 2004 6:26:40 PM]

Share this post


Link to post
Share on other sites
Maybe I''m a complete bonehead, but I don''t think that the constructor will *automatically* clear the memory for you. You should do it *yourself* in the constructor.

-- Wait, wait, wait....who''s Nambla Fett?

Share this post


Link to post
Share on other sites
Ok. A translation unit is basically just the source file (.cpp file) that''s been preprocessed (had all the headers included, macros expanded, etc.). Inside a translation unit, the initialization of global objects is well defined. If object A appears before object B in the same source file, object A will get constructed first.

Between translation units, initialization order undefined by the standard. So if I have A.cpp and B.cpp and Object A lives in A.cpp and Object B lives in B.cpp, then I don''t know if Object A or Object B will get constructed first.

Share this post


Link to post
Share on other sites
quote:
Original post by BASSOFeeSH
Maybe I''m a complete bonehead, but I don''t think that the constructor will *automatically* clear the memory for you. You should do it *yourself* in the constructor.


Right, but if you don''t assign the value in the constructor, then you obviously don''t care what value it had before anyways, so this is pretty much a non-issue.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!