LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2003, 08:18 AM   #1
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Rep: Reputation: 45
need help with pointers


Okey, this is my problem in pseudo code (in c++) :

// ************************* in file1.h **************************************

class someClass
{
private:

int someVar;

public:

someClass();
someClass(int arg);
friend int someFriendFunction();

}

someClass *myClass; // this thing should be reachable everyware.

// ************************* in file1.cpp ***********************************

#include "file1.h"

someClass::someClass()
{
// does nothing
}

someClass::someClass(arg)
{
myClass = this;
}

// ************************** in main.cpp ***********************************

#include "file1.h"

int someFriendFunction()
{
myClass->someVar++;
}

I hope the above code is understandable. I have a class "someClass" which has two constructors. The fist one takes no arguments and exists only to be able to define "myClass".
If somewere else in the code I would do somthing like "someClass foo(3)", "myClass" should point to "foo" and I should be able to manipulate foo anywere.
I can compile and link this code just fine, but the binary gives a seg fault. Why??

Last edited by qanopus; 02-02-2003 at 08:29 AM.
 
Old 02-03-2003, 01:29 AM   #2
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Re: need help with pointers

Quote:
Originally posted by schatoor
// ************************* in file1.h **************************

class someClass
{
private:

int someVar;

public:

someClass();
someClass(int arg);
friend int someFriendFunction();

}

someClass *myClass; // this thing should be reachable everyware.

// ************************* in file1.cpp ***********************************

#include "file1.h"

someClass::someClass()
{
// does nothing
}

someClass::someClass(arg)
{
myClass = this;
}

// ************************** in main.cpp ***********************************

#include "file1.h"

int someFriendFunction()
{
myClass->someVar++;
}
Code:
// I don't think that this is necessary:

someClass::someClass(arg)
{
 myClass = this;
}

//instead just initialize values with the constructor:
someClass::someClass( int arg)
{
  someVar = arg;
}

//Than use your pointer to assign it to an instance of someClass:
someClass *exampleOne = new someClass();
someClass *exampleTwo = new someClass( 20 );

someClass * myClass = exampleOne;
myClass = exampleTwo;

//Also include a destructor in your class
I'm not sure, does this help?

Last edited by GtkUser; 02-03-2003 at 02:32 AM.
 
Old 02-03-2003, 02:40 AM   #3
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Here is an example of a similar program:

******** user.h ********
Code:
#ifndef USER_H
#define USER_H

class myClass
{
 public:

  //constructors
  myClass();
  myClass(int);

  //destructor
  ~myClass() { }

  //behavior
  int getValue();
  void setValue(int);

 private:

  //attributes
  int _value;
};

extern myClass *myPtr;

#endif
******** user.cpp ******
Code:
#include "user.h"

myClass::myClass(){
  _value = 0;
}

myClass::myClass(int value){
  _value = value;
}

int myClass::getValue() {
  return _value;
}

void myClass::setValue(int value){
  _value = value;
}
******* main.cpp ******
Code:
#include<iostream>
#include "user.h"

using std::cout;
using std::endl;

int main()
{
  myClass *myExample_A = new myClass();
  myClass *myExample_B = new myClass(30);

  cout << "myExample_A's value is: " << myExample_A->getValue() << endl;
  cout << "myExample_B's value is: " << myExample_B->getValue() << endl;

  myClass *myPtr = myExample_A;
  myPtr->setValue(100);

  cout << "myPtr's value is: " << myPtr->getValue() << endl;

  return 0;
}
[Bash Prompt]$ g++ -Wall -o myprogram user.cpp main.cpp
 
Old 02-03-2003, 03:45 AM   #4
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
He there. Fist of all, thanks for you reaction.
I realized you can manually point your pointer to the newest variable definition of you class. But I wanted to let this happen automattically when you call the constructor of the class.
But any way, I have been thinking about this and there are neater ways to do what I want. Thanks again for your reaction.
 
Old 02-03-2003, 04:02 AM   #5
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
You coluld make it an attribute of the class and initialize it in the constructor. A pointer to the class object perhaps could be used in the design of a private implementation of a linked list or tree, but I'd have to go to my books for that information. I think that the best way to use it to change attributes of an instance object is to declare it in the function that you need a class pointer and assign it outside of a constructor. The constructor should mostly be used to initialize class attributes (but I'm sure there are exceptions). It might help to be more specific about the context of the problem. If the class pointer is not an attribute of the class, and you want to initialize it to a class instance when the constructor is invoked, and than affect the implementation of the class, that might break encapsulation.

It's an interesting problem, but more information might be necessary.

Last edited by GtkUser; 02-03-2003 at 04:05 AM.
 
Old 02-03-2003, 06:14 AM   #6
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Okey, here is the deal.
I'm making an computer algebra system, kinda like maple or maxima. There is a class called "expression". I your code you do somthing like "expression exp("f: (x)->2+sin(x)");" and an expression is created. The constuctor of "expression" call's a lot of functions. These functions have to have the information which instance fist called the function.
I know I'm being vauge. An example:

class expression
{
public:
/* some stuff */
friend format(....);
privare:
expression(char *anExpression);
int countVars;
};

expression::expression(char *anExpression)
{
format(anExpression, ... /*some other args */);
.....
.....

}

/* someware else in the code */

void format(char *anExpression, .....)
{
/*
detect if there is an independant mathamatical variable defined. In my example "f: (x)->2+sin(x)", that is an "x". If so, increment the "countVars" member of "expression" with one
*/

if(/*detected an independant var*/)
{
expr->countVars++;
/*
Only there is a big problem here. "expr" should point to an instance of the class "expression" that, in it's constuctor, called the function "format". But how do you do that? You could ofcource give the pointer of "countVars" as an argument to "format", but "countVars" is just one example of this problem. So I want to do it in a neater way.
*/
}

I know there are lots of ways around this. In the above example "format" could have been an member function of "expression". The method you sugested in your earlyer post works. I know because I have tried it and it did work (better). I'm just trying to find the best way. And I'm wondering why my original code doesn't work.
 
Old 02-03-2003, 01:17 PM   #7
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Just off the top of my head, don't use a constructor to call functions at all. If you made the argument a string, than the constructor would initialize a c++ string object attribute of the expression class and that's about all.

At the very least, expression should be a method of some maxima class. You could make the method static, for example in Java, the math library methods are static, so that you can call functions without having to create instances:

//Java
Math.round( value );

//You might have something like this:

class Maxima
{
//Psudo code!
static mathfunction1 ( ... );
static mathfunction2 ( ... );
//etc
};

//Than in your code for example

double value = (x * x / Maxima::mathfunction1( 55 - n ) );

Last edited by GtkUser; 02-03-2003 at 01:44 PM.
 
Old 02-03-2003, 04:26 PM   #8
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Cool. Thanks for the tip. Always wonderd what the "static" keyword means.
So I shoulden't call functions in constructors. Okey, I won't. Thanks a million for your time.
 
Old 02-03-2003, 05:09 PM   #9
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
You should probably avoid calling methods or functions in the constructor. On the other hand I could be wrong, but as far as I know, this is true. The static keyword means different things depending on what you apply it to. If you declare a method or an attribute of a class as static than it allows you to call that method or use the attribute without having defined an instance of the class. If the method is static than you can use local variables in the definition of the method, as well as other static attributes of the class. You can't use instance attributes of the class in the static method definition because there might not be an instance of the class object in memory.

Creating your own math classes is not the easiest thing to do, because there are a lot of rules in C++. I'll help you if I can but it's probably over my head.

Last edited by GtkUser; 02-03-2003 at 05:10 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
About pointers RHLinuxGUY Programming 4 11-15-2005 04:47 PM
pointers in c++ marios_auth Programming 1 06-16-2004 08:20 AM
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 09:49 AM
Looking for some pointers... p3ngu!n Linux - Newbie 17 10-30-2003 06:46 AM
pointers in C again h/w Programming 9 10-29-2003 11:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration