|
I am still not too comfortable with passing variables to
constructor, but eventually I'll get over it.
But this got me stumped - never heard of having const used this way. Why?
So how do you read this -X(x) with relation to following function double x()?
Please if this is too basic for you just let someone else answer, OK?
I am asking for help and have no intent to waste your time on trivia.
Thanks
class Point2d{
public:
Point2d() {}
Point2d(double x, double y)
: X(x), Y(y) {}
double x() const { return X; }
double y() const { return Y; }
/**
* Returns the norm of this vector.
* <a href="http://www.codeproject.com/Members/return">@return</a> the norm
*/
double norm() const {
return sqrt( X * X + Y * Y );
}
void setCoords(double x, double y) {
X = x; Y = y;
}
|
|
|
|
|
Using const that way in the declaration of the method tells the compiler that the method is able to be called on constant instances of that object. It indicates that the object is not altered in any way by calling that method. Examples using the class defined above:
const Point2d p1(2,3);
Point2d p2(4,5);
p1.setCoords(3,2);
p2.setCoords(5,4);
double n = p1.norm();
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|
|
|
The link is broken.
Thanks anyway
|
|
|
|
|
It was working this morning, because I read the content to see that it answered the question. Most likely a problem at Microsoft's end; keep trying. Or just use Google to find an alternate page.CodeProject bug, link now fixed.
modified 10-Oct-16 5:02am.
|
|
|
|
|
There's a colon missing between the "https" and "//" in your link - but it's there in the link text. Maybe this a CodeProject bug?
|
|
|
|
|
Thanks, there is a message in Bugs & Suggestions about this, but it is supposed to be fixed now. I will edit my response.
|
|
|
|
|
*i am also new to C++*
i'm not able to figure out where are X and Y defined
i mean the capital ones.
|
|
|
|
|
Ratul Thakur wrote:
i'm not able to figure out where are X and Y defined They aren't. The OP's code is incomplete and thus will not compile.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
how do you initialize this CString array in msvc mfc 2015?
CString weekday[] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};
It worked from msvc++ 2010 but does not work for 2015.
Thanx in Advance
|
|
|
|
|
By not working, do you mean it doesn't compile, doesn't show the values you thought, or someting other?
|
|
|
|
|
It will not compile using msvc++ 2015
|
|
|
|
|
Member 9411471 wrote: It will not compile using msvc++ 2015
And what compiler error does it produce?
|
|
|
|
|
CString weekday[] = {CString ("Sun"), CString( "Mon")...};
|
|
|
|
|
OK, this is really simple question, with example code to boot.
The function pointer declaration is this
void (*gpf_isr)(void) = (0UL);
It is used by this function
void UOTGHS_Handler( void )
{
if (gpf_isr)
gpf_isr();
}
and I access it like so
extern void (*gpf_isr)(void);
then I assign my local poiter / version of the function
gpf_isr = ( object ) . UTOGHS_Handler;
The silly question why this also get pass the compiler , no errors
extern void (*gpf_isr)(void) = (0UL);
The pointer gets reassigned , but ...
|
|
|
|
|
|
That is pretty clear that defining the variable is OK, but if optioned GCC will give a warning.
Kind like deleting a file and being ask "are you sure?"
Thanks
.
|
|
|
|
|
Yes. You may find (on the web) folks arguing that warning is not legitimate.
In a clean implementation you don't qualify with extern the variable in the source file wherein you define it.
|
|
|
|
|
The key point here is the extern qualifier. That states that the variable can be referred to from any module in the final linked executable, but only one instance of it will actually exist.
|
|
|
|
|
C++ has very specific way / syntax to instantiate a class with parameters,
so basic that Google can't find it for me .
I need this for implementing multiple inheritance.
I can instantiate the "top class" in multiple inheritance hierarchy , but it uses system generated default constructors, without parameters of course.
My goal is to create an instance of the top class with parameters passed to the hierarchy lower chain classes.
Do I make sense? I hope so.
Thanks
Vaclav
|
|
|
|
|
|
Quote: but it uses system generated default constructors, without parameters of course Only if you don't provide an implementation of (with or without parameters) the class contructor.
Quote: My goal is to create an instance of the top class with parameters passed to the hierarchy lower chain classes.
Do I make sense? Before attempting the multiple-inheritance step, make sure you understand how class constructor/destructor work.
|
|
|
|
|
class A
{
public :
A( int p ) : a( p )
{
}
protected:
int a;
};
class B
{
public:
B( int q ) : b( q )
{
}
protected:
int b;
};
class C : public A, B
{
public :
C( int x, int y, int z ) : A( x ), B( y), c( z )
{
a = x;
}
private:
int c;
};
int _tmain(int argc, _TCHAR* argv[])
{
C test( 1, 2, 3 );
return 0;
}
Will this help?
|
|
|
|
|
Well, it does and does not help.
I am probably wrong , but I was looking for something like this
class TestClass : paramaterX = "Text"
{
}
I am looking into multiple inheritance and was trying to verify that the classes in the scheme were actually constructed.
Simple
class A : public B,C,D
setup.
So I implemented default constructor and have it to output some text to the console, sort of. It does work.
Now I am trying to find a way to instantiate the scheme with parameters passed to some of the hierarchy classes.
something like
class A : ( class A parameter list ), public B : ( class b parameter list)...
Is that possible ?
BTW I cannot figure out the real syntax / usage of the basic syntax
class A : ?
For example
class A : text
{
};
the compiler wants to see class definition before "text", text is class A variable.
|
|
|
|
|
You could be trying to do several things with the multiple inheritance you need to be very precise here.
You give us "parameterX" which is sort of labelled as text but that could mean a string class, a c string,
or a static const array. You also don't tell us whether the place you want to store the passed text is
in the object new to this class or part of one of the inherited classes, it may matter .. depends.
Do you see how many variations there are on what your simple description says? This is what makes multiple
inheritance tricky and you need to be very clear.
If you search the words "C++ composite class" is that what you are trying to do?
It appears to me you have jumped in at the deep end with C++ classes and trying to do the most complex
without first understanding the basics. I would highly recommend you might like to also do a search of
"C++ Parameterized Constructor" and review it given the code you have written above is nonsense.
I would also recommend you look at
Constructors (C++)[^]
That covers the basics of construction order and explicit and derived constructors in multiple inheritance.
My best guess you are trying to derive the base of a composite class and it would be this
class TestClass {
public:
TestClass(const string& InitText) { text = InitText; };
string text;
};
In vino veritas
modified 3-Oct-16 23:05pm.
|
|
|
|