473,385 Members | 1,588 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,385 software developers and data experts.

How to avoid compiler/linker optimization

Hi,

Is there any way to avoid the compiler/linker to optimize one (and only
this) class member? Below is the code:

<code>
class MyFactory
{
/* . . . */
public static void Register( MyPrototype Proto ) { /* . . . */ }
public static MyPrototype Create( string Name ) { /* . . . */ }
}

class MyPrototype{
/* . . . */
public abstract MyPrototype Clone();
}

class MyClass : MyPrototype
{
/* . . . */
protected static MyClass sm_Instance;
protected MyClass() { MyFactory.Register( this ); }
public MyPrototype Clone() { return (MyPrototype)this; }
}
</code>

Classical Prototype project template. At least in C++. However the compiler
says that the MyClass.sm_Instance member is unused and does not take into
further consideratins. But this member has to guarranty that the prototype
registers itself in the prototype manager (factory). How to solve this
problem?

TIA

--
Roland

Nov 16 '05 #1
4 2043
Roland Zerek <no****@nowhere.com> wrote:
Is there any way to avoid the compiler/linker to optimize one (and only
this) class member? Below is the code:

<code>
class MyFactory
{
/* . . . */
public static void Register( MyPrototype Proto ) { /* . . . */ }
public static MyPrototype Create( string Name ) { /* . . . */ }
}

class MyPrototype{
/* . . . */
public abstract MyPrototype Clone();
}

class MyClass : MyPrototype
{
/* . . . */
protected static MyClass sm_Instance;
protected MyClass() { MyFactory.Register( this ); }
public MyPrototype Clone() { return (MyPrototype)this; }
}
</code>

Classical Prototype project template. At least in C++. However the compiler
says that the MyClass.sm_Instance member is unused and does not take into
further consideratins. But this member has to guarranty that the prototype
registers itself in the prototype manager (factory). How to solve this
problem?


How does MyClass.sm_Instance guarantee that the prototype registers
itself? I don't see how it achieves that at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
> > [...] Classical Prototype project template. At least in C++. However the
compiler
says that the MyClass.sm_Instance member is unused and does not take into further consideratins. But this member has to guarranty that the prototype registers itself in the prototype manager (factory). How to solve this
problem?
How does MyClass.sm_Instance guarantee that the prototype registers
itself? I don't see how it achieves that at all.


OK - the idea is that the static member is initialized at the beginning. So
if the static member is being initialized its constructor is being invoked
and the constructor registers itself in the manager for later use...

However, I am advanced C++ user and rather novice C# so I assumed that the
static member is being initialized at the very beginning of an application.
But it seems it does not. Instead of it it is being initialized at the first
refferrencing try. In fact my idea was not to explicitly do it.

But I still would like to have code that initializes itself at the
beginning. Do any C# solutions exist?

--
Roland
Nov 16 '05 #3
An error occurred in my code
class MyClass : MyPrototype
{
/* . . . */ protected static MyClass sm_Instance;
The line above should present as follow
protected static MyClass sm_Instance = new MyClass();
protected MyClass() { MyFactory.Register( this ); }
public MyPrototype Clone() { return (MyPrototype)this; }
}


--
Roland
Nov 16 '05 #4
Roland Zerek <no****@nowhere.com> wrote:
How does MyClass.sm_Instance guarantee that the prototype registers
itself? I don't see how it achieves that at all.
OK - the idea is that the static member is initialized at the beginning. So
if the static member is being initialized its constructor is being invoked
and the constructor registers itself in the manager for later use...

However, I am advanced C++ user and rather novice C# so I assumed that the
static member is being initialized at the very beginning of an application.
But it seems it does not. Instead of it it is being initialized at the first
refferrencing try. In fact my idea was not to explicitly do it.


Type initializers are executed either "some time before any static
field is first referenced" or "at the first access to any static member
or constructor" depending on the beforefieldinit flag.

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.
But I still would like to have code that initializes itself at the
beginning. Do any C# solutions exist?


Well, you could write some code to examine all the types in all the
loaded assemblies, looking for (say) a specific attribute which you
define to mean "call my type initializer immediately". You could then
hook into AppDomain.AssemblyLoad to apply that procedure to any freshly
loaded assembly too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

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

Similar topics

0
by: Martin | last post by:
The original question is at the bottom: > What OS are you using? Windows 2000 Server SP3 with VS.NET 2003 > How much memory do you have? 512MB, of which typically at least half is...
11
by: Chris Mantoulidis | last post by:
Out of curiosity, I wanted to test executable file sizes of the same program from different compilers... The 2 compilers I tested (both old, on purpose) were Borland C++ 3.1 and DJGPP 2.03... ...
1
by: Peter Forthmann | last post by:
Hi, I am testing the Intel 8.0 Compiler for Linux. For all the sources I have compiled It produces a lot faster code than the GNU g++ compiler. However, I must have something wrong, since I can't...
3
by: Martin | last post by:
Is there a reason for (and hopefully a work-around to) the VC++.NET's compiler "flushing" each output file to disk before compiling the next one? I've noticed this in both the 2002 and 2003...
5
by: Ondrej Spanel | last post by:
I though that inline functions should be always visible only in the compilation unit where they are defined - meaning if compiler cannot inline them, they should be handled as if declared static....
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
9
by: Christof Warlich | last post by:
Hi, consider this: template<typename Tclass X { public: void doSomething(T t); }; int main(void) { X<intx;
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
42
by: jacob navia | last post by:
http://slashdot.org/ "The leaner, lighter, faster, and most importantly, BSD Licensed, Compiler PCC has been imported into OpenBSD's CVS and NetBSD's pkgsrc. The compiler is based on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.