Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

class design problem - simple newbie question 2

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

I have the following code...may be its completed messed up...so please suggest me how to rewrite...

Code:
ClassA.h
______

Class A
{
private:
	A *a;
	char *x;
public:
	A(const char*);
	~A();
	int count(const char*);
};

ClassA.cpp
__________

A::A(const char* y)
{
	a=0;
	x=new char[50];
	strncpy(x,y,50);
}

A::~A()
{
	delete [] x;
	x=0;
}

int A:: count(const char* y)
{
	A a1(y);
	[red]int count = BClassMethod(&a1,0);[/red]
	return count;
}

========================================================

ClassB.h
_________

class B
{
public:
	BClassMethod(A*, A*)
};

I am trying to call the method from class B in class A as shown in red...I am getting error...any suggestions...

-DNG
 
Well, let's engage in these classes design...
This kind of a list of C-style strings node looks as a nightmare:
1. Why 50? Why not 25 or 100? Try to construct A(0) or A(C_string_with_length_greater_than_49). The program crashed...
Better use std::string member or add the code in the A constructor:
Code:
A::A(const char* y):a(0), x(0)
{
    //a=0; // move this assignment to member init list
    if (y) {
        int sz = strlen(y) + 1;
        x = new char[sz];
        memcpy(x,y,sz);
    }
    //x=new char[50];
    //strncpy(x,y,50);
}

2. All A members are private. You can access them only from A member functions. But A has no useful member functions so A objects are real black holes. For example, you can't assign any useful value to a pointer after an object was constructed...

3. What for you invent B class? You can invoke B class non-static method for B class object only. But A class member function count() does not know any B class object variables or references, so it can't invoke BClassMethod(&a1,0) at all!

Free advice: start a new class design from the scratch...

 
thank you I really appreciate your assistance...actually I am trying to implement a doubly linked list and wanted to have two separate classes...one for data dependent functions and the other for data independent functions...

data independent methods will inturn call the data dependent functions...

for example...the two methods...

int count(const char*);--data dependent(inside class A)
int count_object(LinkList*);--data independent (inside class B)

that means either counting based on the string or based on the value pointed by the pointer to the node...

int LinkList::count(const char* cstr)
{
LinkList ps(cstr);
[red]int count = count_object(&ps);[/red]
return count;
}

the above in red fails...since both these methods are in different classes...

can you suggest how to design the class...

thanks

-DNG


 
To call a function in another class without creating an instance of the other class, make the function static. Otherwise, create an object and call it through the new object. If the function has nothing to do with the class to begin with, it shouldn't be in the class; it should be a regular function.
 
Guys,

I fixed the issue with my code...

thanks all for your suggestions...have stars...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top