Click here to Skip to main content
15,883,867 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello ppl. How to call my class object indirect from my object.h header file in to main.cpp file, because i don't want to declare my class object in main.cpp? Which pointer type is need to use better?
For example:
// object.h
MyClass MyObject(1);
int * pMyObject;
pMyObject = &MyObject;


// class.cpp
#include "class.h"
#include "object.h"
MyClass::MyClass
{
...
}


// class.h
Class MyClass
{
...
};


// main.cpp
#include <iostream>
#include "class.h"
#include "object.h"
int main()
{
MyClass MyObject(1); // I need somehow to point my declaration from header,
// but do not declare in main.cpp
MyObject->setFunc(); MyObject->getFunc();
}
</iostream>

Is it possible? Any ideas? Thanks for help.
Posted

1 solution

It looks like you badly misuse header files. Also, you mess up unrelated concepts: separate compilation, access of instances and memory allocation.

It is very bad idea to put any objects in a header file. Put only types and method declarations (and some inline definitions, see below). Just thing of how include works and you will understand why.

This is a usual pattern:

MyClass.h:
C++
class MyClass {
public:
	MyClass(int value) : m_Value(value) { }
	int Value() const { return m_Value; }
	int SomeSeriousMethod();
private:
	int m_Value;
};


MyClass.cpp:
C++
#pragma once
#include <stdafx.h> //only if you use pre-compiled headers
#include "MyClass.h"

int MyClass::SomeSeriousMethod() { /*...*/ }


Usage:
C++
#include "MyClass.h"

//..

void SomeFunction() {
	MyClass instanceOnStack(1);
	int value1 = instanceOnStack.SomeSeriousMethod();
	MyClass * instanceInHeap = new MyClass(2);
	int value2 = instanceInHeap->SomeSeriousMethod();
        //...
        //you can access existing stack instance by pointer as well:
	MyClass * someOtherInstance = &instanceOnStack;
	value2 = someOtherInstance->SomeSeriousMethod();
        //...
	//important; better do it under try-finally; this is a "finally" part:
        delete instanceInHeap;
}


—SA
 
Share this answer
 
v5
Comments
MNMR 30-Mar-11 14:45pm    
A very big thank you SAKryukov!

I will analyze your sample and i will try to undestand how correctly to use that. :)
Sergey Alexandrovich Kryukov 30-Mar-11 15:18pm    
You're very welcome.
Thanks for accepting this Answer.
Call back if your have further question (please put a comment to this post to notify me, use "Improve question" on top or post a new one if the follow-up Question is going to be very different).
Cheers,
--SA
Sergey Alexandrovich Kryukov 30-Mar-11 15:22pm    
I put few minor fixes in the codelets and the text.
--SA
Espen Harlinn 30-Mar-11 15:09pm    
Good effort, 5ed!
Sergey Alexandrovich Kryukov 30-Mar-11 15:20pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900