[QUOTE=nullsquared;20713857][cpp]
/*no const, return values are inherently const*/A A::operator* (const A ¶m) // use a const-ref here to avoid copies
{
// first way
(*this) + param;
// second way (explicit call)
operator+(param);
}
[/cpp][/QUOTE]
OH! So [i]this[/i] is just a pointer! Derp.
Thanks again, mate.
Me again. Anyways, I'm translating my old Mandel code from Java into C++. However, ran into a strange little bug I can't figure out. Obviously, it's going to be something obvious and facepalming-worthy but nevertheless:
[cpp]
class Mandelbrot : public Fractal {
ComplexNumber z, seed;
ushort iterationsUsed;
double bounds[];
static const ushort ITERATIONS = 256;
public:
Mandelbrot(ushort width, ushort height) {
bounds = {-2.25, 0.75, -1.5, 1.5};
};
};
[/cpp]
Gives me errors, whereas:
[cpp]
class Mandelbrot : public Fractal {
ComplexNumber z, seed;
ushort iterationsUsed;
static const ushort ITERATIONS = 256;
public:
Mandelbrot(ushort width, ushort height) {
double bounds[] = {-2.25, 0.75, -1.5, 1.5};
initFract(width, height, bounds);
};
};
[/cpp]
Compiles fine, but I want bounds to be visible to the entire class, not just its' constructor. It also works if I throw it as an instance field to the entire file.
Thoughts? (inb4 obvious problem)
-snip-
You can't assign arrays like that. You can only initialize them like that.
[QUOTE=nullsquared;20722373]You can't assign arrays like that. You can only initialize them like that.[/QUOTE]
So then would this work?
[cpp]
class Mandelbrot : public Fractal {
ComplexNumber z, seed;
ushort iterationsUsed;
double bounds[] = {-2.25, 0.75, -1.5, 1.5};
static const ushort ITERATIONS = 256;
public:
Mandelbrot(ushort width, ushort height) {
};
};
[/cpp]
I thought this didn't work either when I tried it... hrm.
No, that only works for constant statics.
You can, however, do this:
[code]
Mandelbrot(ushort width, ushort height)
{
double arr = { /* whatever */ };
std::copy(arr, arr + 4, bounds); // copy arr into bounds
} /* no ; here btw */
[/code]
I've noticed that most of the tutorials I've looked through declare most of their class methods outside of the class that they belong to (i.e. all non-single-line methods). Is there any specific reason for doing so, or is it merely preference?
Also, thanks again Null.
[QUOTE=Hak;20750191]I've noticed that most of the tutorials I've looked through declare most of their class methods outside of the class that they belong to (i.e. all non-single-line methods). Is there any specific reason for doing so, or is it merely preference?[/QUOTE]
You mean outside of the header file?
That's very useful to keep compilation times to a minimum and reduce code size. Otherwise, every time you include that header file, *all* the code for the class is also included and compiled. If you keep definition (source) separate from declaration (header), most of the code is only compiled once.
[QUOTE=nullsquared;20750281]You mean outside of the header file?
That's very useful to keep compilation times to a minimum and reduce code size. Otherwise, every time you include that header file, *all* the code for the class is also included and compiled. If you keep definition (source) separate from declaration (header), most of the code is only compiled once.[/QUOTE]
I meant stuff like:
[cpp]
class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b) {
x = a;
y = b;
}
[/cpp]
Where the constructor is defined outside the class. Unless that's what you meant.
Yeah, the first part should be in your .h and the second part should be in your .cpp. It keeps things organized. Header is just declaring things; .cpp is actually defining it. You may need to include your header in multiple files so that other .cpp files know about its structure.
[QUOTE=shill le 2nd;20750440]Yeah, the first part should be in your .h and the second part should be in your .cpp. It keeps things organized. Header is just declaring things; .cpp is actually defining it. You may need to include your header in multiple files so that other .cpp files know about its structure.[/QUOTE]
OH I see, that makes sense. Up until now I've just used my header for defines, typedefs, and includes :S
[QUOTE=Hak;20751007]OH I see, that makes sense. Up until now I've just used my header for defines, typedefs, and includes :S[/QUOTE]
I'd say typedefs and includes should go in the header files, since then they can be used from everywhere else. But definitions I'd advise you to seperate into source files.
[editline]03:42AM[/editline]
Oh wait, by defines, you mean #define preprocessor statements right? Yeah, I'd put them in headers too, just so they're declared before they're used.
[QUOTE=lemming77;20754656]I'd say typedefs and includes should go in the header files, since then they can be used from everywhere else. But definitions I'd advise you to seperate into source files.
[editline]03:42AM[/editline]
Oh wait, by defines, you mean #define preprocessor statements right? Yeah, I'd put them in headers too, just so they're declared before they're used.[/QUOTE]
Includes go in the header file if they are used within the header file. If you use std::string in your .cpp, but you declare no functions or define no classes that use std::string in your header, then you should include only in your .cpp.
Make sure to use include guards.
[code]#pragma once[/code] at the the top of the header, or
[code]#ifndef WHATEVER
#define WHATEVER
...
#endif
[/code]
Sorry, you need to Log In to post a reply to this thread.