Passing just one field of a struct as a parameter

Hi, what I want to do is that bool isValidDate function receives only string sFech as parameters instead of the whole struct.

From what I understand I should just write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

bool isValidDate(string sFech)   
{
    bool isValid;

        if(rVenta.sFech == "0")
        {
            isValid = false;
        }
        else
        {
            isValid = true;
        };

        return isValid;
};


Which kinda works, but I'm not sure it's right...

Also, what happpend if I have another struct that has a field called string sFech? Is that a problem at all?


This is the code I'm sure that works, but I don't like at all since there is no need to pass the whole struct. Help appreciated, thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;

typedef struct ty_venta
{
    string sFech;
    int iCant;
    int iCod;
};


bool isValidDate(ty_venta rVenta)   //Corregir para que funcione
{
    bool isValid;

        if(rVenta.sFech == "0")
        {
            isValid = false;
        }
        else
        {
            isValid = true;
        };

        return isValid;
};

void ingreso(ty_venta & rVenta)  
{

    do
    {
        cout << "ingrese fecha del show: " << endl;
        cin >> rVenta.sFech;
    }while(!isValidDate(rVenta));
    
    fflush stdin;
    getchar();
}

void main()
{       

    ty_venta rVenta;

    bool isValid = isValidDate(rVenta);
    ingreso(rVenta);
}
You shouldn't shorten names for no other reason than shortening them. sFecha is just as easy to type, maybe easier.

The first function would work fine. Just call with:

 
bool isValid = isValidDate(rVenta.sFecha);

You should also get out of the habit of the bad Hungarian Notation. Give variables prefixes that have real meaning. Identifying the variable type is not a good reason. Read more here: http://www.joelonsoftware.com/articles/Wrong.html

So, your class might justly be designed thus:

1
2
3
4
5
6
struct venta  // fine, "venta" is a common noun 
{
    string fecha;
    int cantidad;
    int codigo;  // o costo o ?
};

Also, you will notice that you can reduce all that if..else and variable stuff to just:

1
2
3
4
bool isValidDate(const string& fecha)
{
    return (fecha != "0");
}

Since you are working over a specific sale, your main can do that too:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    // Declare the user's sale event
    venta ventaDelUsario;

    // Get information about it from the user
    ingreso(ventaDelUsario);

    // At this point, you know it is valid because ingreso() made sure it was

    ...
}

Hope this helps.
Functions operate on whole types. The function open_door() operates on a whole door. The function fix_door_handle() operates on a door handle, not on a door handle that has specifically been removed from a door (some door handles have never been installed on doors).

I understand your concern that passing the whole structure isn't necessary. In this case you're absolutely right, isValidDate() should probably only take a string (it depends on whether the logic of isValidDate() is intrinsically tied to the fact that the date is part of a ty_venta). However, just because a function operates only on certain members of a type isn't by itself reason enough to have the function take the types of those members.
For example, suppose you have the type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct InventoryItem{
    string name;
    double price;
    int quantity
};

InventoryItem increment_quantity(InventoryItem ii){
    InventoryItem ret = ii;
    ret.quantity++;
    return ret;
}

//usage:
item = increment_quantity(item);
You might argue, following your previous reasoning, that it would be better to just do item.quantity++;.
In general, you'd be wrong. If the implementation of InventoryItem were to change the nature of quantity, you'd either have to find all places where the value is changed manually, or get clever with operator overloading. Neither are exactly nice solutions.

Anyway, more to the point of your question, your modification is correct if you make this change:
if(rVenta.sFech == "0")
sFech is, from the point of view of isValidDate(), not part of any struct. It's just some random string floating out there.

Soon enough you'll learn that in reality, these "optimizations" are irrelevant. There are ways of passing data of any size without any kind of performance hit.
Topic archived. No new replies allowed.