Skip to main content
GameDev.net gamedev.net
🔒 Locked

Constructor problems

Started by robinei Feb 1, 2004 at 6:20 PM 6 replies 400+ views
Original Post
robinei
robinei
Is there any reason why one of the things below works and the other doesn't?

// This works:

Vector2d pos(0.0f, 0.0f);
Vector2d vel(10.0f, 10.0f);
Particle p1(pos, vel, 10.0f);

// This doesn't:

Particle p1(Vector2d(0.0f, 0.0f), Vector2d(10.0f, 10.0f), 10.0f);

[edited by - robinei on February 1, 2004 7:24:04 PM]
DJSnow
DJSnow
please show which operators of your classes are overloaded.

DJSnow
---
this post is manually created and therefore legally valid without a signature
DJSnow---this post is manually created and therefore legally valid without a signature
robinei
robinei
vector2d.h

#ifndef VECTOR2D_H
#define VECTOR2D_H

#include <cmath>

class Vector2d {
public:
Vector2d() : m_x(0.0f), m_y(0.0f) {
}
Vector2d(float x, float y) : m_x(x), m_y(y) {
}
Vector2d(Vector2d &vec) : m_x(vec.m_x), m_y(vec.m_y) {
}

Vector2d operator + (Vector2d &vec) {
Vector2d tmp;
tmp.m_x = vec.m_x + m_x;
tmp.m_y = vec.m_y + m_y;
return tmp;
}
Vector2d operator - (Vector2d &vec) {
Vector2d tmp;
tmp.m_x = vec.m_x - m_x;
tmp.m_y = vec.m_y - m_y;
return tmp;
}
Vector2d operator * (float scale) {
Vector2d tmp;
tmp.m_x = m_x * scale;
tmp.m_y = m_y * scale;
return tmp;
}
float operator * (Vector2d &vec) {
return (vec.m_x * m_x) + (vec.m_y * m_y);
}

void operator += (Vector2d &vec) {
m_x += vec.m_x;
m_y += vec.m_y;
}
void operator -= (Vector2d &vec) {
m_x -= vec.m_x;
m_y -= vec.m_y;
}
void operator *= (float scale) {
m_x *= scale;
m_y *= scale;
}

void operator = (Vector2d &vec) {
m_x = vec.m_x;
m_y = vec.m_y;
}

float Length() {
return sqrt((m_x * m_x) + (m_y * m_y));
}

void Unit() {
float inv_len = 1.0f / Length();
m_x *= inv_len;
m_y *= inv_len;
}

float X() {
return m_x;
}
float Y() {
return m_y;
}
private:
float m_x, m_y;
};

#endif

particle.h:
   

#ifndef PARTICLE_H
#define PARTICLE_H

#include "vector2d.h"
#include "cppsdl.h"

const float g = 9.81f;

class Particle {
public:
Particle(Vector2d pos, Vector2d vel, float mass) : m_pos(pos), m_vel(vel), m_acc(0.0f, 0.0f), m_mass(mass) {
}

void Attract(Particle ∥) {
float d = (m_pos - par.m_pos).Length();
float F = (g * m_mass * par.m_mass) / (d * d);
float acc = F / par.m_mass;
Vector2d tmp = m_pos;
tmp -= par.m_pos;
tmp.Identity();
tmp *= acc;
m_acc += tmp;
}

void Update() {
float delta = (float)SDL::Timer::Delta() / 1000.0f;
Vector2d tmp = m_acc;
tmp *= delta;
m_vel += tmp;
tmp = m_vel;
tmp *= delta;
m_pos += tmp;
}

float X() {
return m_pos.X();
}
float Y() {
return m_pos.Y();
}
private:
Vector2d m_pos;
Vector2d m_vel;
Vector2d m_acc;
float m_mass;
};

#endif



[edited by - robinei on February 1, 2004 7:42:38 PM]
SiCrane
SiCrane
Try changing your Vector copy constructor to take const references instead of non-const references. i.e.:
Vector2d(const Vector2d &vec)
robinei
robinei
That worked! I could never imagine that would be significant. Thanks!

[edited by - robinei on February 1, 2004 7:46:56 PM]
Doc
Doc
It''s significant since it''s an on-the-spot construction. Just like the number 0.0f is a constant, so is Vector2d(0.0f, 0.0f) a constant. It only becomes non-const when you give it a variable name. And that''s why your copy constructor should take const references.
SiCrane
SiCrane
Vector2d(0.0f, 0.0f) isn''t a constant, it''s a temporary. And temporaries cannot be l-values, which is what you need if you want to use a non-const reference. However, even though it can''t be bound to non-const references a temporary is still not const -- you can call non-const member functions on a temporary. This is what lets the std::vector "swap trick" work.
nosfuratu
nosfuratu
a copy constructor has to take in a reference parameter

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.