Classes: Properties

I'm about to rip my hair out. I have a class that is supposed to encrypt/decrypt and string. I had it declared in temporary instances (crypt_class([string]).encrypt([password])), but that was detrimental to the performance, so I created 1 instance in the function (seeing as it was used throughout, and it would be deconstructed anyway), which should speed it up (and it did significantly!).

Here's the problem, though: it seems to be encrypting and decrypting improperly now. I've tested it, and found that adding void operator=(const crypt_class&) member got it to work... but then from there I don't know what happens, the program saves, and when it tries to reload the data it fails (the data is not encrypted the same for some reason).

A test revealed somthing (and to be quite frank, I have a lot of very colorful words I want to throw around...):

1
2
3
4
5
6
7
8
9
crypt_class c;
c.set_pass(password);
c = [a string];
[a string] = c.decrypt();

//c.encrypt() and c.decrypt will return the decrypted and encrypted
//strings respectively.
cout<< "string = "<< [a string]<< endl;
cout<< "c = "<< c.encrypt()<< "   decrypted: "<< c.decrypt()<< endl;


And the most peculiar thing was displayed: for both decrypted and encrypted values, the same string was shown, but the string that was "decrypted" was not the same.

I'm going mad over this. I will try to put together some working code when I have time... hopefully tomorrow. In the meantime mabey someone knows somthing about how classes work that could explain such 'behavior'...

Here is some stuff about the class I wrote, if it helps:

- All arguments passed to member functions are passed by const values, and by address...

- returning member variables is done by the following syntax: return this->[var]

- constructors: (const string& and const char* are used to set a string variable... this variable is used to store text that is decrypted and encrypted, and this variable is modified when this happens.

I ask for speculation until I can put somthing together, that can be run...

Thanks for your time.

Last edited on
Either use a debugger, or you'll need to show up more code. Specifically, the members and methods of crypt_class.
Alright. I am thinking of modifying it a differen way though. As of right now, the encryption and decryption uses an iterator to go through each element of the strig of text (which is supposed to be data we want protected) and encrypts/decrypts it directly, modifying the string member of the class, as well as returnig it's value at the end of the function. This is the only point at which somthing could occure, because I've teted all of the other member functions and they should work perfectly (seeing as I already have a terribly inefficient verion up and running... And working). Mabey it has somthing to do with iterator pointers (after all, it looks like a pointer, acts mostly like a pointer, there must be one somwhere... Refer me to a text i I'm wrong, Because I wouldn't know what to search for).

Also, I probably should mention that static variable aren't used.

Forgive the gramtical errors, I'm on my ipod, as today I'm maintaning my computer.
I figured it out. It had to do with the assignment operators.

New question:

Is there any difference in the way assignment is executed between using constructors and overloaded operators? If there is, which takes precedence, the operator or the constructor?

ex:

1
2
3
4
5
6
7
8
9
10
11
class example_class{
public:
    example_class(const string&); //creates a default (=) operator... didn't work out in my program though...
    void operator=(const string&);  //an overloaded operator, but it's allowed by the compiler.

};

//in some function:
example_class ex = "an example"; //does it use the constructor or the overloaded operator??

ex = "Another example";  //I assume this uses the operator, but I am uncertain of this. 
Last edited on
Topic archived. No new replies allowed.