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++ / May 2006

Tip: Looking for answers? Try searching our database.

Quick question:

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robby - 28 May 2006 17:30 GMT
Hello:

Can we pass a pointer to an object from one cpp file to another by using a
function call. Assuming the object was created and declared along with its
accessor methods in the first cpp file, but you need to manipulate the same
object in the second cpp file.

heres a quick example of the function call which calls a function
in the second cpp file:

==================cpp #1
#include "cpp2.h"
class components
{
public:
void SYS_innitz_FLAG_AllTrueInCase() {}
virtual void SYS_set_SFCC(bool bCondTrue,int AmtOfComm) {}
protected:
};

static components cp;

SFC1(&cp);   //Calling a function which exists in cpp #2
=======================

==================cpp2.h - header for cpp2
void SFC1(components *cp);   //Function declaration
======================
==================cpp #2
void SFC1(components *cp)    //Function implementation
{}
======================

When I do this I get the obvious errors of:

c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\SFC_1.h(19): error
C2065: 'components' : undeclared identifier

c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\SFC_1.h(19): error
C2065: 'cp' : undeclared identifier

It seems that all cpp files *must* have their own class definitions, and
associated implementations... right? I do beleive that I did post this
problem before at a much larger scale, however, there could be a wrinkle of
difference in this one. Basically, just double checking!

All welcome and appreciated for the feedback....

thanks!

Signature

Best regards
Robert

Carl Daniel [VC++ MVP] - 28 May 2006 19:46 GMT
> Hello:
>
> Can we pass a pointer to an object from one cpp file to another by
> using a function call. Assuming the object was created and declared
> along with its accessor methods in the first cpp file, but you need
> to manipulate the same object in the second cpp file.

Yes, of course.  There's nothing magical about what is in a single source
file.  If your code is properly structured (i.e. you're not violating the
one definition rule anywhere and you're #including the right headers
everywhere) then there's no difference between putting your entire program
in a single source file or putting each and every function in a separate
source file.

> heres a quick example of the function call which calls a function
> in the second cpp file:
[quoted text clipped - 26 lines]
> c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\SFC_1.h(19):
> error C2065: 'components' : undeclared identifier

> c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\SFC_1.h(19):
> error C2065: 'cp' : undeclared identifier

Remember that the compiler sees only what you "feed" to it.  Does cpp #2
#include the definition of the class "components" anywhere?  If not, then
the compiler can't see it.

> It seems that all cpp files *must* have their own class definitions,
> and associated implementations... right?

No.  A .cpp file is simply a unit of code that the compiler processes - but
each .cpp file is processed in isolation.  Nothing that's declared in one
file is visible in another file.  That's why we have #include and the
convention of using "header files" - common definitions and declaration are
placed in header files that are then included into all of the compilation
units that need them.

The most common way of structuing C++ programs is to put class definitions
into header files and the implementation of functions (be they member
functions or free functions) into compilation units (i.e. ".cpp files").
That way you can #include the defintion of a class into as many compilation
units as you require.

-cd

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.