Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2005

Tip: Looking for answers? Try searching our database.

Managed code, .H and .CPP

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ignazio - 09 Nov 2005 15:41 GMT
When creating forms with Visual C++ 2005, all the code for building the
interface (the InitializeComponent 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
Carl Daniel [VC++ MVP] - 10 Nov 2005 03:46 GMT
> When creating forms with Visual C++ 2005, all the code for building
> the interface (the InitializeComponent method) and event handlers are
[quoted text clipped - 4 lines]
> 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
Ignazio - 10 Nov 2005 13:36 GMT
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::WriteLine("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

> > When creating forms with Visual C++ 2005, all the code for building
> > the interface (the InitializeComponent method) and event handlers are
[quoted text clipped - 13 lines]
>
> -cd
Carl Daniel [VC++ MVP] - 10 Nov 2005 15:06 GMT
> Thank you. If I understood well, I can write all the code of the
> forms in their .H without any problem.
[quoted text clipped - 24 lines]
> 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
Ignazio - 10 Nov 2005 15:24 GMT
Thank you.
Ignazio

> > Thank you. If I understood well, I can write all the code of the
> > forms in their .H without any problem.
[quoted text clipped - 31 lines]
>
> -cd
Peter Oliphant - 10 Nov 2005 21:16 GMT
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==]

> Thank you. If I understood well, I can write all the code of the forms in
> their .H without any problem.
[quoted text clipped - 48 lines]
>>
>> -cd
David Anton - 10 Nov 2005 15:11 GMT
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).
Signature

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

> When creating forms with Visual C++ 2005, all the code for building the
> interface (the InitializeComponent method) and event handlers are set in the
[quoted text clipped - 6 lines]
> Thank you,
> Ignazio
Ignazio - 10 Nov 2005 15:26 GMT
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

> 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
[quoted text clipped - 14 lines]
> > Thank you,
> > Ignazio
David Anton - 10 Nov 2005 15:41 GMT
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.
Signature

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

> Yes, but there's some problem with that approach:
> 1. maybe even unmodified files are recompiled each time (the .cpp changes
[quoted text clipped - 23 lines]
> > > Thank you,
> > > Ignazio

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.