Example:

class foo
{
	public:
		void DoNothing()
		{
			cfee.AddNothing();
		}
};

class fee
{
	public:
		void AddNothing()
		{
			int a = 0;
			a += 1;
		}
}cfee;

Is there a way to call a function in a instance of a class that was declared after the creation of the first class?

It has become a hassle to have 2 classes interact since one always comes before the other.

yes you separate the class definition and the member definitions like this

// This is normally in a header file foo.h
class foo
{
	public:
		void DoNothing();
};
// This is normally in a separate cpp file foo.cpp
void foo::DoNothing()
{
    cfee.AddNothing();
}

That way the cpp files can include the definitions of any classes they use and any class can use any other class.

That is why it is standard practice to implement a class in 2 files, 1 header file and 1 cpp file and to only use those files for defining the class in question.

I do have my code that way for all my classes, even If I define the function after the instance of the class it gives a unresolved external symbol error.

Edit:
Is there a example of the standard in which I should be declaring my classes.

Fixed unresolved error, I had forgotten the parameters for creating a instance.

Anywho, class header declaration standard example anyone?

Not really it is your choice or the choice of where you work.

The main thing is to be consistent with what you do.

Here is a sample header file:

should have:
1- description to ensure that you understand your design
2 - meaningful class name the same as the .h file name
you can have iterator classes in the same header
3 - function names that make sense
4- comments on how to use function on line above
5 - use of public: private: etc
6 - const where necessary

/*
Add description of what your file is doing
in this instance a demo class to show
that inherits all of the functions and member variables from
base foo
*/

#pragma once //MS code guarding to ensure only used once
#include "fee.h" //include the types you use in the function
#include "base_foo.h" //base class

class foo : public  base_foo
{
public:
  foo(); //public constructor so can create instance
  //this comment to be used by intellisense
  void DoSomething();
  //const as it will not change any member variables
  fee get_my_fee() const;
  void set_fee(const fee &cfee){my_fee = cfee;} //no semi colon 
protected:
    //can be used by any inherted class
   virtual void do_nothing();
private:
 fee my_fee //member variable
};//semi colon needed

foo.h has to define any function that your code will want to use

#include "foo.h"
#include <iostream>

//call other constructors using: 
foo::foo()
:my_fee()
{
}

fee foo::get_my_fee() const
{
 return my_fee;
}

void foo::do_nothing()
{
  std::cout << "you can overwrite me with inheritance" << std::endl;
}

//adding one to 'a' in fee
void foo::do_something()
{
 /*further comments which don't display with intellisense*/
 my_fee.AddNothing();
}

now you could have accessed do_nothing using a global
and extern and placed the cfee initialisation in the .cpp

you just have to be careful that you don't go round in infinite loops in constructors when integrating two classes

another option is to have static methods

#include <string>

class test
{
private:
   test();
public:
   static std::string say_hello();
};

now with static functions can be used without an instance

std::string str = test::hello();

but they can only use static member variables

Not really it is your choice or the choice of where you work.

The main thing is to be consistent with what you do.

Alrighty, one more for the road.

say I make a function that is a char pointer,
in the function I malloc into a char pointer a sizable amount of memory.
Then return the pointer.

How do I free the memory I malloced if it has already returned?

edit: also without returing it into another pointer and then free() the pointer it was returned to.

Alrighty, one more for the road.
say I make a function that is a char pointer,
in the function I malloc into a char pointer a sizable amount of memory.

new is preferable way of creating memory

this is a design decision once you call new somewhere the memory must be deleted. sometimes one class will create the memory while another will be incharge of deleting it.

//gives control of new block in return
char * my_function()
{
char * p_memblock = new char[big_size];
/*
other stuff
*/
return p_memblock;
}

if your function does somthing like this then the function no longer has responsibility for the pointer
if p_memblock was a member variable you can clean it up with delete[] say in the destructor but if it was a local then

{
 char *pc = my_function();
 /*use pc
 etc..*/
 //now clean up
 //finished with pc so delete
 delete [] pc;
 pc = 0;
}

normally new and delete will be as close together as possible

I want to avoid having to create a pointer to delete.

Pretty much its a function that takes in a string such as "%d", and the parameters. Then returns the string since I do not like using sprintf().

Edit: tetron, you over complicated my example to a point which it makes no sense to me.

If you want to return a string then...

return a string :D

OK sorry it sound fucisious but what I mean is do not return an allocated array of characters actually return a string object. That hides all the allocation, decallocation errors for you.

In suggest inside your function you use a ostringstream object to create your string, although I guess you may be calling vsprint or similar.

Anyway you can still return a string.

This is C++ start getting used to and using the faciities it provides rather than trying to rely on older C methods.

Lets see if i have your memory question correct this time:

you have a char * that you want to pass into a function set some values then return the char * Now I see a couple of issues with you question that lead to me being slightly confused.

1 - you can only empty a variable when everything has finished with it
you could assign a "" tp the pointer if new wasn't used but c++ has classes that would make your life easier.

If you say want to manipulate a char * to make an output then you really want to use std::string this is a useful class that is like a char 8 and takes care of some of the handling anyway

#include <string>
#include <iostream>
#include "my_class.h"

int main()
{
char * pc = "hello world"; // a normal cahr *
std::string str; 
str = pc; // a string can take a char * 
//you can also directly output with cout
std::cout << str << std::endl;

//define a second string using the constructor
std::string str2("good bye world");
// a class to hold some functions
my_class c;
c.write(str1),

//turn str 2 into "good bye world hello"
str2 = c.add_hello();
//a function taking a string
c.write_c_string(pc);
c.wrtie_c_string("my name");
//the .c_str() gets the same type as char*
c.write_c_string(str2.c_str());
//can empty str2 with
str2.clear();
//haven't used new so no delete
return 0;
}

now we have three text strings to send to a function to write them
and a method that has a return of string the functions can take a reference & so

#include <string>

class my_class
{
  my_class();
   void write(const std::string &str);
   void write_c_string(char * pc);
   void write_c_string(std::string &str);
   std::string add_hello(std::string &str);
};

now to define some of the functions

#include <iostream>
#include "my_class.h"

my_class::my_class()
{
}

void my_class::write(std::string &str)
{
   std::cout << "I say: " << str << std::endl;
}

void my_class::write_c_string(char *pc)
{
std::cout << pc << std::endl;
//could add pc = "";
}

void my_class::write_c_string(sdt::string &str)
{
std::cout << str << std::endl;
//could add str.clear();
}

std::string my_class::add_hello(std::string &str2)
{
//copy str2 into ret
 std::string ret(str2);
ret += " hello";
//if you wish you can empty str2  in here ...
//str2.clear();
return ret;
//once ret has returned it is emptied but a copy can be assigned
}

This probably wasn't a clear example either as there are lots of points to address with your question.

- once a function returns a copy of data is put in a new variable.
So if it was a pointer with new or malloc it would have to be deleted later.

- the variable can be emptied once it has been sent to the function as it is no longer needed

I want to avoid having to create a pointer to delete.

Pretty much its a function that takes in a string such as "%d", and the parameters. Then returns the string since I do not like using sprintf().

Edit: tetron, you over complicated my example to a point which it makes no sense to me.

I don't have any errors, I just wanted to keep my program efficient by releasing unused memory.

I just ooze strange errors and problems.
Like my thread creation.

Engine.Win.m_hCalcThread	= CreateThread( 0, 0, ( LPTHREAD_START_ROUTINE )CalcThread,  0, 0, 0);

Worked yesterday, but now I converted my code to a win32 project and run a dialogbox instead of a main window.
Now the thread never gets created. It gives off no error and if I try to print text to my allocated console after the call of create thread, the console never displays it.


Edit: also for anyone confused.
This is the function I was talking about:

char* ssprintf( char* szString, ... )
{
	char* szBuffer = ( char* )malloc( 1024 );
	
	va_list va_alist;
	va_start( va_alist, szString );
	vsprint f( szBuffer, szString, va_alist);
	va_end ( va_alist );
	
	return szBuffer;
}

Edit: also for anyone confused.

That would include me - your questions bounce around alot for a single thread:)

This is the function I was talking about:

char* ssprintf( char* szString, ... )
{
	char* szBuffer = ( char* )malloc( 1024 );
	
	va_list va_alist;
	va_start( va_alist, szString );
	vsprint f( szBuffer, szString, va_alist);
	va_end ( va_alist );
	
	return szBuffer;
}

There is nothing to clean up at this point although you should not use malloc but new As the function has a return the returned char * now has responsibility

Engine.Win.m_hCalcThread	= CreateThread( 0, 0, ( LPTHREAD_START_ROUTINE )CalcThread,  0, 0, 0);

Worked yesterday, but now I converted my code to a win32 project and run a dialogbox instead of a main window.
Now the thread never gets created. It gives off no error

does it return a valid HANDLE ?

try writing to an edit or message box rather than to console.
There are a lot of changes going to a win32 project

if( !Engine.Win.m_hCalcThread )
		MessageBox( 0, "Problem", 0, 0 );
	else
		MessageBox( 0, "No problem", 0, 0 );

I added this after the creation call.

No message box is ever shown.

Edit: I call the thread creation before the window creation and it works now. Very very strange...

A final thought for things that can go awry is when your initialisation is using global variables.

I have had Visual Studio 2003 fail to build global variables in the order that it was told. If you are doing auto registering classes or the like this can cause unexpected behaviour.

It might be worth explicitly checking any globals you use in initialisation are correct.

Also the other thing I have found is - it is often worth rebuilding a solution from scratch. So that all of the inclusions etc have to be redone. With Visio I have sometimes had the system misbehave to the point where you have to start again rather than trying to find the phantom error.

This odd behaviour has sometimes been contributed to by my antivirus.

Edit: I call the thread creation before the window creation and it works now. Very very strange...

Yes I found that it creates the thread usually as the first thing right off the bat so I have to sleep the thread at the beginning since it doesn't go in the order in which I set it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.