Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 operator=???
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lone]2




PostPosted: Sat Apr 16, 2005 4:33 pm   Post subject: operator=???

hallooooo im back again with another question >.<

Okay this is The code INSIDE the main program
code:

page = Layout(40, 50);

now... inside my object declaration
i have a member function
code:

Layout& Layout::operator=(const Layout& right)
{
        Layout(right.height, right.width);
        return *this;
}


but the statement in my main program does not call the operator= .......... what am i doing wrong?[/code]
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Apr 16, 2005 4:45 pm   Post subject: (No subject)

code:
Layout& Layout::operator=(const Layout& right)
{
   Layout(right.height, right.width);
   return *this;
}


Let's look at what this is actually doing. It takes a constant Layout reference. That's all well and good, but what you do next is pointless. You create a new Layout object, and then immediately discard it. You then simply return this.

It looks to me like you're trying to create a copy constructor. Let's look at a simple copy constructor.

code:
class Foo
{
  private:
    int bar;
  public:
    Foo(const int init_bar);
    Foo(const Foo& other);

    int get_bar() const;
};

Foo::Foo(const int init_bar) : bar(init_bar) { }

Foo::Foo(const Foo& other) : bar(other.get_bar()) { }

int Foo::get_bar() const
{
  return bar;
}
Lone]2




PostPosted: Sat Apr 16, 2005 4:55 pm   Post subject: (No subject)

hmm.... i do have a copy constructor... but... i want an assignment operator now..

code:

Layout& Layout::operator=(const Layout& right)
{
   Layout(right.height, right.width);
   return *this;
}

doesn't this code calls the objects constructor?
for example
page = Layout(40,50);
calls the page should call the operator= function...
and operator= function should call the constructor (page's constructor)
(i have a constructor that takes 2 of the same parameters)
and i KNOW its not calling the operator= function because... when i do this:
code:

Layout& Layout::operator=(const Layout& right)
{
   printf ("\nklsajdsad\n");
   Layout(right.height, right.width);
   return *this;
}

nothing shows up on the screen
(i know theres cout but i just used this to test it out, and i included stdio.h so it should work no problem)
wtd




PostPosted: Sat Apr 16, 2005 5:00 pm   Post subject: (No subject)

No, it doesn't.

code:
Layout(a, b);


Simply creates a new object. Since you don't do anything with that object, it gets discarded immediately. My guess is that you'd want something like:

code:
Layout& operator=(const Layout& other)
{
  height = other.height;
  width = other.width;
  return *this;
}


This is phenomally bad, though. You're changing the meaning of a frequently used operator where you should be using a copy constructor.

Also, having height and width public is probably a symptom of bad design.
Lone]2




PostPosted: Sat Apr 16, 2005 5:35 pm   Post subject: (No subject)

well... i already constructed a object though... and when im done with the object... i want to 'rebuild' it with different values.... thats why im not using the copy constructor.. and thats why im using the assignment operator.....
oh yea...... as for public height and width .... im IN the object...(in Layout object) so its still not valid to access the variable as "other.height"? does that mean i have to write accessor functions...???
...
so... now... i written a "set" function which should take all of my problems.....
but when i do:
code:

Layout page; //did something with the object
// now want to rebuild it by using assignment operator
page = Layout(40,50);

STILL does not work..... it DOESN'T call the assignment function...
also... i tried doing this:
code:

Layout page;
page.operator=(Layout(40,50));

it gives me an compile error
" 'operator =' is not a member of 'Layout' in function main ()"

so... which i don't really understand.. because i have a prototype in my class...:

Layout& operator=(const Layout& right);

and i also defined my function:
Layout& Layout::operator=(const Layout& right)
{
set(right.height, right.width);
return *this;
}
XD... C++ is so troublesome
wtd




PostPosted: Sat Apr 16, 2005 5:41 pm   Post subject: (No subject)

It sounds like you really need to work on more fundamental design issues. May I see your code in its entirety?
Lone]2




PostPosted: Sat Apr 16, 2005 5:59 pm   Post subject: (No subject)

okay... well.. heres the layout object...

code:

using namespace std;
/*********************************************************************************
Struct: Rect

Purpose: To hold

public members:
        left            Holds the left part of the rectangle
        top                    Holds the top part of the rectangle
        right         Holds the right part of the rectangle
        bottom      Holds the bottom part of the rectangle
        *txt            WwText Reference
*********************************************************************************/
struct Rect
{
        int left;
        int top;
        int right;
        int bottom;
        WwText *txt;
};

class Layout
{
        char **lines;
        Rect *rects;
        int height;
        int width;
        int size;
public:
        Layout();
        Layout(int h, int w);
        Layout(const Layout& in);
        void set(int h, int w);
        int add(int left, int top, int right, int bottom, WwText &txt);
        void generate();
        const char* line(int i);
        bool fwriteln(FILE *fp, int i);
        Layout& operator=(const Layout& right);
        ~Layout();
};

Layout::Layout()
{
        lines = 0;
        rects = 0;
        height = 0;
        width = 0;
        size = 0;
        set(60,80);
}

Layout::Layout(int h, int w)
{
        lines = 0;
        rects = 0;
        height = 0;
        width = 0;
        size = 0;
        set(h,w);
}

Layout::Layout(const Layout& in)
{
        lines = 0;
        rects = 0;
        height = 0;
        width = 0;
        size = 0;
        set(in.height, in.width);

}
Layout::~Layout()
{
        if(lines)
        {
                for (int i = 0 ; i < height; i++)
                {
                        if (lines[i])
                                delete [] lines[i];
                }
                delete [] lines;
        }
        if (rects)
        {
                delete [] rects;
        }
}

void Layout::set(int h, int w)
{
        size = 0;
        if(lines)
        {
                for (int i = 0 ; i < height; i++)
                {
                        if (lines[i])
                                delete [] lines[i];
                }
                delete [] lines;
        }
        if (rects)
        {
                delete [] rects;
        }
        if (h > 0)
                height = h;
        if (w > 0)
                width = w;
        rects = new(nothrow) Rect[50];
        lines = new(nothrow) char*[height];
        if (lines)
        {
                for (int i = 0; i < height; i++)
                {
                        lines[i] = new(nothrow) char[width + 1];
                        if (lines[i])
                        {
                                for (int j = 0; j < width; j++)
                                {
                                        lines[i][j] = ' ';
                                }
                                lines[i][width] = '\0';
                        }
                }
        }
}

int Layout::add(int left, int top, int right, int bottom, WwText &txt)
{
        int result = 0;
        if (size < 50)
        {
                if ((right - left) >= 0 && (bottom - top) >= 0)
                {
                        if (right >= width)
                                right = width - 1;
                        if (right < 0)
                                right = 0;
                        if (left >= width)
                                left = width - 1;
                        if (left < 0)
                                left = 0;
                        if (top >= height)
                                top = height - 1;
                        if (top < 0)
                                top = 0;
                        if (bottom >= height)
                                bottom = height - 1;
                        if (bottom < 0)
                                bottom = 0;
                        rects[size].left = left;
                        rects[size].right = right;
                        rects[size].top = top;
                        rects[size].bottom = bottom;
                        rects[size].txt = &txt;
                        size++;
                        result = size;
                }
        }
        return result;
}

void Layout::generate()
{
        char *temp = 0;
        for (int i = 0; i < size; i++)
        {
                temp = new(nothrow) char[rects[i].right - rects[i].left + 2];
                *rects[i].txt = rects[i].right - rects[i].left + 1;
                if (temp)
                {
                        for (int j = rects[i].top; j <= rects[i].bottom; j++)
                        {
                                rects[i].txt->getline(temp);
                                int x = 0;
                                for (int k = rects[i].left; k <= rects[i].right; k++)
                                {
                                        if (temp[x])
                                        {
                                                lines[j][k] = temp[x++];
                                        }
                                        else
                                        {
                                                lines[j][k] = ' ';
                                        }
                                }
                        }
                }
                delete [] temp;
        }
}

const char* Layout::line(int i)
{
        char *temp = 0;
        if (i >= 0 && i < height)
        {
                temp = lines[i];
        }
        return temp;
}

bool Layout::fwriteln(FILE *fp, int i)
{
        bool check = false;
        if (i >= 0 && i < height)
        {
                fprintf(fp, "%s\n", line(i));
        }
        return check;
}

Layout& Layout::operator=(const Layout& right)
{
        printf("kjashdlakshdlkasdlsakhdlaskd");
        set(right.height, right.width);
        return *this;
}
wtd




PostPosted: Sat Apr 16, 2005 6:11 pm   Post subject: (No subject)

Oy. Problem number 1: You're not writing C++. You're writing "C with classes". If someone is teaching you this, he or she should be fired.

code:
#include <string>
#include <iostream>
#include <fstream>


Are the three most helpful lines of code I can show you.
Sponsor
Sponsor
Sponsor
sponsor
Lone]2




PostPosted: Sat Apr 16, 2005 6:27 pm   Post subject: (No subject)

lol .... yes... im aware im writing c code in c++.... but.... this is like a class thing.. and we just learned c... so i guess they want to make the transition from c to c++ smoother?....
and i actually found out my problem........
im so stupid.... Cool
i forgot to change my definition in my layout.h
-_- how stupid of me....
wtd




PostPosted: Sat Apr 16, 2005 6:45 pm   Post subject: (No subject)

The problem is that C and C++ are different languages. There is no transition.
wtd




PostPosted: Sat Apr 16, 2005 7:13 pm   Post subject: (No subject)

The program you're writing... could you explain what it is you're trying to do? I have no idea what WwText is, for instance.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: