473,799 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Managed code, .H and .CPP

When creating forms with Visual C++ 2005, all the code for building the
interface (the InitializeCompo nent method) and event handlers are set in the
..H file, as they were inline methods. So I ask, for managed code they are
effectively inline methods or they are handled indifferently if they are in
the class declaration or outside of it? Do I still have to write declarations
and definitions in different files for avoiding multiple compiling when
including the headers or it is handled more like C# for managed code?

Thank you,
Ignazio
Nov 17 '05 #1
8 1448
Ignazio wrote:
When creating forms with Visual C++ 2005, all the code for building
the interface (the InitializeCompo nent method) and event handlers are
set in the .H file, as they were inline methods. So I ask, for
managed code they are effectively inline methods or they are handled
indifferently if they are in the class declaration or outside of it?
Do I still have to write declarations and definitions in different
files for avoiding multiple compiling when including the headers or
it is handled more like C# for managed code?


Methods defined in the class body are (I believe) NOT implicitly inline in a
managed class - they have to be represented in the IL as methods, and then
it's up to the JIT to inline them if it decides to.

That said, methods defined in the class definition are linked "as if" they
were inline, just as with standard C++, and fairly analogously to C#.

-cd
Nov 17 '05 #2
Thank you. If I understood well, I can write all the code of the forms in
their .H without any problem.
The only problem is with cross-dependance of two classes. For example:

// A.h
#include "B.h"

ref class A {
void f () {
B::g();
}
void g () {
Console::WriteL ine("test");
}
};

// B.h
#include "A.h"
ref class B {
void g() {
A::g();
}
};

The compiler encounters an error, as it is right to do in C++. So, it is
necessary to write a header and a cpp file for one of the two classes...
Don't you find it a little tricky that I need just the .H for some files and
both for other files?

Ignazio
"Carl Daniel [VC++ MVP]" wrote:
Ignazio wrote:
When creating forms with Visual C++ 2005, all the code for building
the interface (the InitializeCompo nent method) and event handlers are
set in the .H file, as they were inline methods. So I ask, for
managed code they are effectively inline methods or they are handled
indifferently if they are in the class declaration or outside of it?
Do I still have to write declarations and definitions in different
files for avoiding multiple compiling when including the headers or
it is handled more like C# for managed code?


Methods defined in the class body are (I believe) NOT implicitly inline in a
managed class - they have to be represented in the IL as methods, and then
it's up to the JIT to inline them if it decides to.

That said, methods defined in the class definition are linked "as if" they
were inline, just as with standard C++, and fairly analogously to C#.

-cd

Nov 17 '05 #3
Ignazio wrote:
Thank you. If I understood well, I can write all the code of the
forms in their .H without any problem.
The only problem is with cross-dependance of two classes. For example:

// A.h
#include "B.h"

ref class A {
void f () {
B::g();
}
void g () {
Console::WriteL ine("test");
}
};

// B.h
#include "A.h"
ref class B {
void g() {
A::g();
}
};

The compiler encounters an error, as it is right to do in C++. So, it
is necessary to write a header and a cpp file for one of the two
classes... Don't you find it a little tricky that I need just the .H
for some files and both for other files?


Yep.

You can thank C++'s one-pass compilation model for that. Until (and unless)
C++ adopts a multi-pass compilation model we'll always have that limitation.

-cd
Nov 17 '05 #4
There's an approach outlined by Stephen Fraser where you place all the code
in .h files (using #pragma once), and then in a single .cpp file you #include
all those .h files in the appropriate order.

This allows you to avoid the archaic separation of definition and
implementation (which had it's reasons 20 years ago, but is unnecessary with
modern IDE's).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"Ignazio" wrote:
When creating forms with Visual C++ 2005, all the code for building the
interface (the InitializeCompo nent method) and event handlers are set in the
.H file, as they were inline methods. So I ask, for managed code they are
effectively inline methods or they are handled indifferently if they are in
the class declaration or outside of it? Do I still have to write declarations
and definitions in different files for avoiding multiple compiling when
including the headers or it is handled more like C# for managed code?

Thank you,
Ignazio

Nov 17 '05 #5
Thank you.
Ignazio

"Carl Daniel [VC++ MVP]" wrote:
Ignazio wrote:
Thank you. If I understood well, I can write all the code of the
forms in their .H without any problem.
The only problem is with cross-dependance of two classes. For example:

// A.h
#include "B.h"

ref class A {
void f () {
B::g();
}
void g () {
Console::WriteL ine("test");
}
};

// B.h
#include "A.h"
ref class B {
void g() {
A::g();
}
};

The compiler encounters an error, as it is right to do in C++. So, it
is necessary to write a header and a cpp file for one of the two
classes... Don't you find it a little tricky that I need just the .H
for some files and both for other files?


Yep.

You can thank C++'s one-pass compilation model for that. Until (and unless)
C++ adopts a multi-pass compilation model we'll always have that limitation.

-cd

Nov 17 '05 #6
Yes, but there's some problem with that approach:
1. maybe even unmodified files are recompiled each time (the .cpp changes
because even only one .h is modified, and all the .h get recompiled)
2. look at the reply to Carl Daniel for the message I wrote about
cross-dependancy.

Thank you,
Ignazio

"David Anton" wrote:
There's an approach outlined by Stephen Fraser where you place all the code
in .h files (using #pragma once), and then in a single .cpp file you #include
all those .h files in the appropriate order.

This allows you to avoid the archaic separation of definition and
implementation (which had it's reasons 20 years ago, but is unnecessary with
modern IDE's).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"Ignazio" wrote:
When creating forms with Visual C++ 2005, all the code for building the
interface (the InitializeCompo nent method) and event handlers are set in the
.H file, as they were inline methods. So I ask, for managed code they are
effectively inline methods or they are handled indifferently if they are in
the class declaration or outside of it? Do I still have to write declarations
and definitions in different files for avoiding multiple compiling when
including the headers or it is handled more like C# for managed code?

Thank you,
Ignazio

Nov 17 '05 #7
Right. I'm not so concerned about the excessive compilation, but the
cross-dependency issue is the show stopper to that approach.

I'm still waiting for the day when C++ compilers sort out these dependency
issues (like C#, VB, Java, etc.). Having worked a lot in these other
languages, it really does seem like a major step backwards to have to worry
about cross dependencies.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"Ignazio" wrote:
Yes, but there's some problem with that approach:
1. maybe even unmodified files are recompiled each time (the .cpp changes
because even only one .h is modified, and all the .h get recompiled)
2. look at the reply to Carl Daniel for the message I wrote about
cross-dependancy.

Thank you,
Ignazio

"David Anton" wrote:
There's an approach outlined by Stephen Fraser where you place all the code
in .h files (using #pragma once), and then in a single .cpp file you #include
all those .h files in the appropriate order.

This allows you to avoid the archaic separation of definition and
implementation (which had it's reasons 20 years ago, but is unnecessary with
modern IDE's).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"Ignazio" wrote:
When creating forms with Visual C++ 2005, all the code for building the
interface (the InitializeCompo nent method) and event handlers are set in the
.H file, as they were inline methods. So I ask, for managed code they are
effectively inline methods or they are handled indifferently if they are in
the class declaration or outside of it? Do I still have to write declarations
and definitions in different files for avoiding multiple compiling when
including the headers or it is handled more like C# for managed code?

Thank you,
Ignazio

Nov 17 '05 #8
Hi Ignazio,

Personally, I try to put everything in the .H file. The reason is it makes
the generated code in-line (performance) and all in one place (source
organization). With so much RAM in computers nowadays it usually makes sense
to optimize for speed rather than size, and in-line usually contributes in
this regard.

However, there are times you MUST put stuff in a .CPP file. This is when you
have two classes which both must reference each other in a 'detailed' way
(i.e., each calls a method in the other). If just the pointer is needed to
an instance of another class, one can still stay in the .H by declaring the
other class before it is used without defining it.

That is, this is ok to stay in two different .H files:

--------------------------------
----------------------------------
header file A:

#include "B.h"
class A
{
void Method_A()
{
m_B_Ptr->Method_B() ;
} ;
:
B* m_B_Ptr ;
} ;
--------------------------------
header file B:

class A;
class B
{
:
A* m_A_Ptr ;
} ;
-------------------------------
-------------------------------
But this is NOT ok:
-------------------------------
-------------------------------
header file A:

#include "B.h"
class A
{
void Method_A()
{
m_B_Ptr->Method_B() ;
} ;
:
B* m_B_Ptr ;
} ;
-----------------------------
header file B:

class A; // replacing this with #include "A.h" causes circular definition
errors
class B
{
void Method_B()
{
m_A_Ptr->Method_A( ) ; // error since doesn't know about Method_A
}
:
A* m_A_Ptr ;
} ;
-----------------------------
-----------------------------
This can be corrected by changing the B.H file and adding a B.CPP like so:
-----------------------------
-----------------------------
header file B:

#include "A.h" ; // change declaration to included A.h

class B
{
void Method_B() ; // declaration only
:
A* m_A_Ptr ;
} ;
------------------------------
CPP file B:

#include "B.h" ;

ClassB::Method_ B()
{
m_A_Ptr->Method_A( ) ; // no error
}
-----------------------------
-----------------------------

Note that B.CPP gets its definition of class A from the include of A.h in
B.h.

Also, it is STRONGLY recommended you place a "#pragma once" at the top of
each .H file (but never in a .CPP file) which means if it occurs more than
once in a definition the compiler will ignore all but one include of this
header file. That way you can have more than one .H file included in the
same definition, all of which have a common .H included (e.g., program
equates/defines header), file without problems...

[==P==]

"Ignazio" <Ig*****@discus sions.microsoft .com> wrote in message
news:EE******** *************** ***********@mic rosoft.com...
Thank you. If I understood well, I can write all the code of the forms in
their .H without any problem.
The only problem is with cross-dependance of two classes. For example:

// A.h
#include "B.h"

ref class A {
void f () {
B::g();
}
void g () {
Console::WriteL ine("test");
}
};

// B.h
#include "A.h"
ref class B {
void g() {
A::g();
}
};

The compiler encounters an error, as it is right to do in C++. So, it is
necessary to write a header and a cpp file for one of the two classes...
Don't you find it a little tricky that I need just the .H for some files
and
both for other files?

Ignazio
"Carl Daniel [VC++ MVP]" wrote:
Ignazio wrote:
> When creating forms with Visual C++ 2005, all the code for building
> the interface (the InitializeCompo nent method) and event handlers are
> set in the .H file, as they were inline methods. So I ask, for
> managed code they are effectively inline methods or they are handled
> indifferently if they are in the class declaration or outside of it?
> Do I still have to write declarations and definitions in different
> files for avoiding multiple compiling when including the headers or
> it is handled more like C# for managed code?


Methods defined in the class body are (I believe) NOT implicitly inline
in a
managed class - they have to be represented in the IL as methods, and
then
it's up to the JIT to inline them if it decides to.

That said, methods defined in the class definition are linked "as if"
they
were inline, just as with standard C++, and fairly analogously to C#.

-cd

Nov 17 '05 #9

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

Similar topics

1
3560
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
16
14141
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
2
4682
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
2
7302
by: asanford | last post by:
We use StackWalk(StackWalk64) from dbghelp.dll to walk our callstacksas needed, using the various Sym* methods (SymGetSymFromAddr, SymGetLineFromAddr) to resolve source file, function name, and source line number for each frame. This works fine for unmanaged code, but when we have a call stack that goes into or through some managed code, we cannot resolve any info for the managed frames - we can however, get all the information for the...
4
5724
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage collected. I also noticed that when replaced some of the existing MFC dialogs with managed winforms that everything is still running in the same app domain.( No context change) So my question is this, what are the performance differences in using...
3
1619
by: Steve Marsden | last post by:
Hi I am an experienced C programmer and we have a large app written in C which I have just recompiled with /clr to start to add in managed code to use .NET framework and windows forms bit by bit. I am new to managed C ++(and C++ for that matter). I am struggling with the fact that it seems you cannot have global managed variables although I think I understand why you can't.
12
1632
by: doug | last post by:
I understand the basics of what managed code offers and that you open yourself up to security issues if you allow unmanaged code. We already have a decent amount of VB6 code to include COM DLLs. If we put wrappers around some of our code or leave some "asis" what makes our existing production code 'evil' just because it is now considered 'unmanaged'? It may seem like a simple niave question, but the definitions for managed and...
5
3419
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most part attempting to do what is this article by Nish: http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw I have to hook up a unmanaged callback to a managed method using IJW NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
25
3021
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class MyClass:IDisposable
3
4713
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting enhancement which asynchronly
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.