Results 1 to 7 of 7

Thread: Properties?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    what's the equivilend of a VB property in C++?

    ie if I have a Simple class which does nohing but hold a number, something like this

    class cNumber
    {
    public:

    long Value; //Stored Data

    void cNumber(void); //Constructor

    };

    void cNumber::cNumber(void){}


    how would I put validation code for Value without changing the interface, ie so I can still go

    cNumber objMyInstace;

    objMyInstance = 10;

    thnx in advance

  2. #2
    Guest
    You can't do it like that,

    From c++ perspective:
    but in your case you can do
    objMyInstance.Value=10;
    which isn't exactly desirable in c++

    what you should do is
    class cNumber
    {
    private:
    int value;
    public:
    cNumber()
    {
    value=10;//some default
    }
    void SetVal(int val)
    {
    value=val;
    }
    int GetVal()
    {
    return(value);
    }
    };

    if you want to implement a function that you can call something like

    Sub f()
    dim f as myC++class
    call f.myC++function()
    end sub

    that is whole other story.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Alright, Thanks.

    I thought there might be some way of overloading a member function so it acted like a variable. Oh Well can't win em all.

  4. #4
    Lively Member
    Join Date
    Jan 1999
    Location
    Rochester NY, USA
    Posts
    93
    Technically you can make that work but it requires overriding what "=" does. For that class, you can make the "=" behave differnetly.. My C++ really sucks(I'm an ANSI C person) so I'm not going to attempt to give specific code.. but here's an example of a situation...

    char *ReturnSomeString(void) {

    char *localstring = "This is my string";
    char *tmpCharPtr *, *t;

    tmpCharPtr = malloc(sizeof(localstring)+1)
    if tmpCharPtr == 0 {
    /* do err handleing */
    return NULL;
    }
    t = strncpy(tmpCharPtr, localstring, sizeof(tmpCharPtr));
    if t = 0 {
    /* do err handleing */
    return 0;
    }
    return tmpCharPtr;
    }

    You can make the "=" operator, when refrencing your class, perform this. So instead of allocating mem and copying the string, you could make it so the "=" operator does this. It's cool but dangerous.


  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    OK, I think I get what you're talking about, even though It'll take me a few readthroughs to get what's going on (because I'm still new to C++, I'm not insulting your code)

    Thanks a lot for your help.

  6. #6
    Lively Member
    Join Date
    Jan 1999
    Location
    Rochester NY, USA
    Posts
    93
    OK.. I'll try to explain.. this is just C.. no c++ stuff in it..

    /*Function that returns a pointer to a string */
    char *ReturnSomeString(void) {

    /*
    declaring a char pointer that points to "This is my string"
    */
    char *localstring = "This is my string";
    /*
    Declaring 2 char pointers.. they don't point to anything at this point
    */
    char *tmpCharPtr *, *t;

    /*
    allocating memory with malloc.. you can use "new" in c++.. it's the size of the string localstring +1.. assign the pointer to this memory to tmpCharPtr
    */
    tmpCharPtr = malloc(sizeof(localstring)+1)

    /*
    if the pointer "tmpCharPtr = 0 then that means that it didn't allocate mem.. there's an err.. we need to trap this.
    */
    if tmpCharPtr == 0 {
    /* do err handleing */
    return NULL;
    }

    /*
    strncpy is declared like this..
    strncpy(char *to, char *from, size).. you have to give it a pointer to the original string and the destination.. and the size. it copies a string from one location to another.
    */
    t = strncpy(tmpCharPtr, localstring, sizeof(tmpCharPtr));
    /*
    same premise as malloc
    */
    if t = 0 {
    /* do err handleing */
    return 0;
    }
    /*
    returns a pointer to the string we copied
    */
    return tmpCharPtr;
    }


    OK.. a little more explanation here. you can declare a character array as

    char mystring[] = "This is another string";

    It's pretty much the same.. but not quite.. as ..

    char *mystring = "This is another string";

    The reason you use strncpy is because you can't really assign character pointers to one another. if we tried to do that in the function above, there would be a major problem.. heres why..

    localstring is a pointer that points to a string. It was declared in that function. when you declare something, it only "sticks around" as long as the code is still within the same scope. Scope's are defined by the braces.. here they are the beginning and the end of the function. As soon as we leave the function, the memory is returned to the system. So if we did a..

    return localstring;

    it would return the pointer that was in localstring. But now that we're not in the function anymore, that memory could contain anything.. more than likely something at sometime is going to use it for something else. You have no idea what's going to be at the memory location when you go to use it. The best you could hope for is something garbled.. more than likely it's going to cause some type of crash. Now when we malloc memory, it sticks around until we "free" it. So what we do is malloc memory the size of the string we want to return, copy the desired string to that memory location and return a pointer to it.


    Does that make a little more sense now? As far as overriding an operator, what I was saying as an example was that you could make the = operator perform all of this when you are referring to a certian class. The problem is that someone may look at what you're doing and think it's all screwed up.

    I'm not going to be able to give you much advice or help in the area of C++. I'm trying to get more familiar with it for various reasons but Up until about 3 months ago, I had hardly even looked at it. One thing of note though... all the stuff I wrote up there can be included in C++.. that's one of the nice things.. you have access to all the C libs.
    Anyway.. let me know if there's something in praticular here that is confusing..

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Thanks a lot, I've got something about overloading operators written down somewhere. That should help a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width