|
I was wondering if somebody could tell me how self registering objects work when we use a anonymous namespace and static keywords. Is the registration done during compile time or run-time??
So, if i declare
//Create an Anonymous for the factory to self register frame classes, also such that the scope is limited to this file preventing the compiler from getting confused with methods of other frame types
namespace
{
Frame* createIFrame()
{
return new IFrame;
}
int FrameType = 1;
bool registered = FrameFactory::Instance()->registerFrameFn(1,createIFrame);
}
this code is inserted into the .cpp file of every class that is added new, how does the object registration occur without ever being called explicitly??
|
|
|
|
|
A method in an anonymous namespace is visible only to the cpp file in which it is written. It cannot be called from anywhere else.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
The code sample is taken from Modern C++ Design by Andrei Alexandrescu. It works fine, registering the classes during compile time, why it works I do not understand. As you an anonymous namespace inside a cpp files that has the class definition,cannot be called externally.
But this method is called by ClassName::register and works fine.
|
|
|
|
|
I'm not sure I understand the scope of the code in question, but I suspect that Koenig lookup is the reason, if you're saying what I think you're saying.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I think I did not phrase my question properly, let me try again:
factory.h
class Factory
{
.......
}
Rectangle.cpp //This implements the rectangle class, and will register itself with the factory
namespace
{
Shape *createRectangle()
{
return new Rectangle;
}
Factory::Instance()->registerShape(rect_id);
}
main.cpp
int main
{
Shape *s
s=Factory::Instance()->createShape(rect_id)
}
Factory is a singleton and Instance is the single point of entry. The part I do not understand is this, there is no explicit call to registerShape, it is inside the anonymous namespace. Every new shape will just include similar code to the one in the namespace, and by some compiler magic all these new shapes will get registered with the Factory with their id.
What exactly is the compiler magic, that register each new shape, does this happen dynamically or at compile time?
|
|
|
|
|
I think I did not phrase my question properly, let me try again:
factory.h
class Factory
{
.......
}
Rectangle.cpp //This implements the rectangle class, and will register itself with the factory
namespace
{
Shape *createRectangle()
{
return new Rectangle;
}
Factory::Instance()->registerShape(rect_id);
}
main.cpp
int main
{
Shape *s
s=Factory::Instance()->createShape(rect_id)
}
Factory is a singleton and Instance is the single point of entry. The part I do not understand is this, there is no explicit call to registerShape, it is inside the anonymous namespace. Every new shape will just include similar code to the one in the namespace, and by some compiler magic all these new shapes will get registered with the Factory with their id.
What exactly is the compiler magic, that register each new shape, does this happen dynamically or at compile time?
|
|
|
|
|
What do registerShape and createShape look like ? I can't see how this would work, either. There is no compiler magic to make methods call themselves.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
hello can any body tell me whether there is ne library in c++ using which i can display a jpg image in c++ output window.or send me ne code for this program.
thanx
|
|
|
|
|
http://www.codeproject.com/bitmap/cximage.asp
|
|
|
|
|
Hi all,
I'm trying to show a form using show but I'm getting some odd behaviour. Here's the code,
<br />
SplashScreen* splash = new SplashScreen ();<br />
splash->Show ();<br />
<br />
if ( ! BCM->ValidateConfigurationFile () )<br />
{<br />
return 0;<br />
}<br />
else<br />
{<br />
if ( BCM->RunLoginScreen () )<br />
{<br />
Application::Run ( new NetworkView ( BCM->getAccessLevel () ) );<br />
}<br />
return 0;<br />
I want the splash form to stay open until it is closed in the RunLoginScreen () method. The form however doesn't open until the login method opens a login screen. Can anyone tell me why I'm getting this behaviour.
Thanks in advance guys.
|
|
|
|
|
Right, new probelm.
Apparently it wasn't a problem with the show method, it's a problem with the pictureBox. I can't seem to use a pictureBox whilst using the form->Show () method. If I use ShowDialog () then alls good but this isn't what I need to do.
Does anyone have any idea why I can't use, or more correctly, why my pictureBox images won't show when I use form->Show (); Or an alternative way to go.
|
|
|
|
|
Given the declaration. int *a; The statement. a = new int [50]; Dynamically allowcates an array of 50 components of the type?
int, int*, pointer, address?
|
|
|
|
|
You get a pointer to an array of 50 unique integers.
|
|
|
|
|
int* a; declares a as a pointer to an int .
a = new int[50] allocates an array of 50 int , and then returns the address of the array in memory into a.
then, you can code a[1] to access the 2nd cell on the array.
if you wanted an array of pointers to int, you should write this :
int* a;
a = new int*[50];
cheers,
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Not quite. That would be:
<br />
int **a = new (int *)[50];
<br />
int *b = a[0];
<br />
int c = 0;
<br />
b = &c;
<br />
a[0] = &c;
<br />
*b = 5;
<br />
*(a[0]) = 5;
(Note: I wrote this code in the input box and didn't compile it to double-check if it works, so if I got something wrong please correct me.)
Like the other posters said, the code "int *a = new int[50]" allocates an array of 50 integers on the heap and assigns a to the address of the beginning of that array.
|
|
|
|
|
Thank You very much I wish I would of found this site Last year because I Graduated today from Farmingdale suny I thank all comrades of this site Thank You
|
|
|
|
|
Question:
typedef void (* FUNCTION_PTR) (float*, float**); //.h
float InArray[10];
float* OutArray[5];
float first_element, second_element;
(*FUNCTION_NAME)(InArray, OutArray); //Calls the function
OutArray_first_element=??????????????//how do I access Out Array elements?
OutArray_second_element=????????????//how do I access Out Array elements?
Thanks, Mike
|
|
|
|
|
OutArray[0][0] gets the first element.
OutArray[0] gives you the first array, then the second [0] indexes it. You have an array of arrays there. The real question is how do you know the dimensions.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
OutArray has a size of 8 and all are floats.
InArray's (float) size I control. I can only access the
app through this DLL, so I can't see their dimensions and typing.
I see the first OutArray element
just fine. I've tried many dereferencing and array
indexing tricks and I still see no data on the other elements,
assuming I'm hitting them. Any other ideas?
Can you assume an dimension and give me an example
to be sure I've got it right?
Thanks, Mike
|
|
|
|
|
Well, you need to do OutArray[0][x], because it's a ** ( assuming that it's an array of 8 dimensions that was passed in as a pointer ). If that doesn't work, then something else is broken.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi all,
I've got an application that I've added language capabilties by means of resource files and cultures. This works ok and I have no problem with it what-so-ever. I now want to add another resource that holds all the images my application will use. My problem comes whenever I try to use the resource. My app complains about the images resource or when I manage to sort that problem it complains about the language resource.
I have had both working on occasion but it seems to be very flakey and sometimes chooses not to work for no apparent reason other than the project being closed over night. Could someone explain to me the exact way in which to add .resX files to projects and what setting should be set in the project configuration because I think I may have misunderstood the process somewhere along the road.
Thanks in advance.
Richie
|
|
|
|
|
I've been reading about this recently in Chris Sells book, "Windows Forms Programming in C#". He has a 30 page chapter on the subject in which he describes in detail how to embed resources into a .NET Framework executable.
The process is a little different in C++, and. in all probability, depends on which version Visual Studio .NET you are using.
If you haven't tried it already, what happens when you open the compiled executable in Ildasm.exe? The manifest should show your resources and the associated namespaces. This is from Chris's book: "To check that a file has been embedded properly into your project's output assembly, you can use the .NET Framework SDK tool ildasm.exe. This tool shows all embedded resources in the Manifest view of your assembly".
Depending on the method that you used to embed the resource, he goes on to describe the process that Visual Studio .NET uses to assign the default namespace and the actual path to the resource. He also points out that building your project causes the .resx data to be embedded as nested resources, which are resources grouped into a named container. He then goes on to describe several methods for loading these resources.
You don't say in your post, but, how are you attempting to access these resources?
|
|
|
|
|
Thanks for the pointer. I think it was to do with the project configuration. I went through it and changed it where required and it seems to be ok (for now...). I had to change the resource output file names in configuration. I now know to use the ildasm.exe to see if they are being embedded correctly.
Thanks again.
Oh and I was using the ResourceManager class to access them.
|
|
|
|
|
I have C++ libraries which i want to use in C application.
Please help.
Lata Agrawal
|
|
|
|
|