1

I have a class Image, and I need to return instance of this class. What's the simplies way to do it? I'm a java programmer and "return this" giving me an error.

#include <string>
#include "AEEImage.h"

using namespace std;

IImage *image;
Image *img;

Image::Image()
{
    image = new IImage();
}

Image::~Image()
{
    delete image;
    image = NULL;
}

 Image *createImage(string str)
{
    return /*return instance of this class*/;
}

Here's definition of Image:

#pragma once

#include <string>
using namespace std;

class Image
{
public:
    Image();
    ~Image();
    Image createImage(string str);
    Image createImage(const Image *img, int x, int y, int with, int height, int transform);
    Image createImage(istream is);
};

I have to write a class in C++ similiar to Image class in JAVA ME (link): -these same name of class (Image) -these same name of methods, type of arguments and return types

Mariusz Chw
  • 364
  • 2
  • 19
  • 6
    Forget Java, get [a good book](http://tinyurl.com/so-cxxbooks), read it, and come back afterwards. – Xeo Nov 15 '12 at 12:27
  • 3
    Please, post a class declaration. And what are you using global variables for?.. – Mikhail Nov 15 '12 at 12:28

4 Answers4

8

this is only available in non-static member functions; they are called on an object, and this points to that object. createImage() is a non-member function; it's not called on an object, and so there is no this pointer.

Presumably, it's supposed to create an image and return a pointer to that. Perhaps you want:

return new Image;

Remember that it's the caller's responsibility to delete the object once it's no longer needed - there is no garbage collection in C++. You would be wise to study RAII to learn how to automatically manage dynamic objects in C++; in particular, a smart pointer would do the trick here:

std::unique_ptr<Image> createImage() {
    return std::unique_ptr<Image>(new Image);
}

In general, you should forget any idioms you learned in Java, since that is a very different language. There are many good books on C++.

You should also avoid global variables, especially for things that logically belong to an instance of a class. It looks very much like IImage *image; should be a member of Image rather than a global variable (although perhaps it already is; I can't tell, since you haven't posted the definition of Image).

UPDATE: you now say that createImage is a member, and that the class should emulate the Java class you link to. In that case, it should be static. For a class member, that has roughly the same meaning that it does in Java: it is associated with the class itself, not any particular object. Then the definition needs to qualify the name with Image:: to indicate that it is a member, not a different function declared in the namespace. So you want something like:

class Image {
public:
    // some members
    static Image * createImage(string); // Add "static" to match the Java class.
                                        // Return a pointer (or a smart pointer)
};

Image * Image::createImage(string) {    // Add "Image::" to indicate it's a member
    return new Image;
}

// Usage example:
Image * image = Image::createImage("whatever");  // "::" rather than Java's "."

Note that returning Image rather than Image * (or a smart pointer) will return a copy of the Image object, which is (probably) not what you want; that's another big difference from Java, where class objects are always passed by reference(*). Pointers are the closest things that C++ has to Java's object references.

(*) Note to pedants: by "passed by reference", I mean the object itself is not cloned; the object reference is passed by value.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • In Java, Objects are NEVER passed by reference. They are always passed by value. So many people get this wrong. Please read [PassByValue](http://javadude.com/articles/passbyvalue.htm). – MWid Nov 15 '12 at 13:17
  • 1
    @MWid: Yes, I know that Java's object references are passed by value; I was using "pass by reference" to mean that the objects themselves are never cloned, as they are in C++. – Mike Seymour Nov 15 '12 at 13:18
4

You've declared that function as a free one when it needs to be a member function. Only member functions have a this pointer.

class image {
   //...
   Image *createImage(string str)
   //...
};

Image *Image::createImage(string str)
{
    return this;
}

Although if it's a create function your logic is probably wrong and you shouldn't be using this.

Pubby
  • 51,882
  • 13
  • 139
  • 180
2

First, createImage(..) has to be a member function of class Image, as mentioned before. Second, there are three ways to return the current object, by pointer, by reference and by value:

class Image
{
    public:

    //by Pointer
    Image* PointerToImage()
    {
        return this;
    }
    const Image* ConstPointerToImage() const
    {
        return this;
    }
    //by Reference
    Image& ReferenceToImage()
    {
        return *this;
    }
    const Image& ReferenceToImage() const
    {
        return *this;
    }
    //by Value
    Image CopyOfImage() const
    {
        reuturn *this;
    }
}

Hope this helps.

Philipp
  • 649
  • 2
  • 7
  • 23
-1

You don't need to return the instance, you already have it when you call createImage?

Mulder
  • 197
  • 3
  • 12