Forward declarations and #pragma managed/unmanaged

G

Gustavo L. Fabro

Greetings!

Going directly to the point:

myclass.h:
//--------------------------------------

#pragma managed

//Forward declaration
class AnotherClass;

#pragma unmanaged

class MyClass
{
public:
void func1(AnotherClass* ptr);
}

Will this '#pragma managed' before 'class AnotherClass' have any effect at
all on my native class? I know the forwad declaration only puts the symbol's
name under the symbols table, but I was wondering if, being defined under
'managed' setting, this symbol would have any thunking layers or anything
like that that would make it's access slower.

Thanks,

Fabro
 
C

Carl Daniel [VC++ MVP]

Gustavo said:
Greetings!

Going directly to the point:

myclass.h:
//--------------------------------------

#pragma managed

//Forward declaration
class AnotherClass;

#pragma unmanaged

class MyClass
{
public:
void func1(AnotherClass* ptr);
}

Will this '#pragma managed' before 'class AnotherClass' have any
effect at all on my native class? I know the forwad declaration only
puts the symbol's name under the symbols table, but I was wondering
if, being defined under 'managed' setting, this symbol would have any
thunking layers or anything like that that would make it's access
slower.

Based on a very simply experiment, it doesn't appear to make any difference.
What matters is whether #pragma managed is in effect where the class
definition appears.

<code>
#pragma managed

//Forward declaration
class AnotherClass;

#pragma unmanaged

class MyClass
{
public:
void func1(AnotherClass* ptr) {}
};

#pragma unmanaged // 1

class AnotherClass
{
public:
void f() {}
};
</code>

If the line at //1 is #pragma unmanaged, the resulting .obj looks identical
to that produced if // 1 is deleted. It // 1 is #pragma managed,
AnotherClass::f() is emitted as managed code.

-cd
 
G

Gustavo L. Fabro

Hmmm

I took your idea and compiled these files

#1

#pragma managed
class AnotherClass;
#pragma unmanaged

class MyClass
{
public:
void func1(AnotherClass* ptr);
}

#2

#pragma unmanaged
class AnotherClass;
#pragma unmanaged

class MyClass
{
public:
void func1(AnotherClass* ptr);
}

The obj files have the same size but are different. But that doesn't tell
*me* much, because If I compile the same file twice, different (binary
speaking) obj files will be created too.

Fabro
 
C

Carl Daniel [VC++ MVP]

Gustavo said:
Hmmm

I took your idea and compiled these files

#1

#pragma managed
class AnotherClass;
#pragma unmanaged

class MyClass
{
public:
void func1(AnotherClass* ptr);
}

#2

#pragma unmanaged
class AnotherClass;
#pragma unmanaged

class MyClass
{
public:
void func1(AnotherClass* ptr);
}

The obj files have the same size but are different. But that doesn't
tell *me* much, because If I compile the same file twice, different
(binary
speaking) obj files will be created too.

Use dumpbin /all on the .OBJ files to see the internals. I didn't do a very
thorough examination, but at the level I did look, it appeared that no
significant changes to the file were made. In your example, there's no code
being generated at all, so I think you need to embelish the test case just a
bit to make it meaningful.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top