ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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??
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
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.
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.
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;
};
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.
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 ) );
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.