Say I want to check to see whether a subclass has implemented one of it's parent's virtual functions (never mind whether this smells of bad architecture... it's an exercise). If I wanted to see if two regular functions were identical, I could just check &f == &g.
// Plain old functions
void f() {}
void g() {}
...
std::cout << "&f " << &f << "\n"; // "1" OK, for some reason func ptrs are converted
std::cout << "&g " << &f << "\n"; // "1" to booleans when printed. I can dig it.
std::cout << "&f == &g " << (&f == &g) << "\n"; // "0" Good, &f and &g are unequal as expected.
But with virtual member functions, behavior is different.
// Base class with a virtual
struct A {
virtual void f() {}
};
// Subclass which implements f
struct B : public A {
void f() {}
};
// Subclass which doesn't implement f
struct C : public A {};
...
std::cout << "&A::f " << &A::f << "\n"; // "1"
std::cout << "&B::f " << &B::f << "\n"; // "1"
std::cout << "&C::f " << &C::f << "\n"; // "1" ... okay ... annoying, but alright.
std::cout << "&A::f == &B::f " << (&A::f == &B::f) << "\n"; // "1" DANGER - why does &A::f == &B::f if &f != &g?
std::cout << "&A::f == &C::f " << (&A::f == &C::f) << "\n"; // "1"
std::cout << "&B::f == &C::f " << (&B::f == &C::f) << "\n"; // "1"
std::cout << "(void*)&A::f " << (void*)&A::f << "\n"; // "0x4084b0" Here's what I was actually looking for.
std::cout << "(void*)&B::f " << (void*)&B::f << "\n"; // "0x4084bc" Good - the B::f differs from A::f as it should
std::cout << "(void*)&C::f " << (void*)&C::f << "\n"; // "0x4084b0" Perfect.
std::cout << "(void*)&A::f == (void*)&B::f " << ((void*)&A::f == (void*)&B::f) << "\n"; // "0"
std::cout << "(void*)&A::f == (void*)&C::f " << ((void*)&A::f == (void*)&C::f) << "\n"; // "1"
std::cout << "(void*)&B::f == (void*)&C::f " << ((void*)&B::f == (void*)&C::f) << "\n"; // "0" These are the comparison results I want
So my question is marked by DANGER in the code above. Why does &A::f == &B::f if &f != &g? Is there a way to do the comparison I want without casting to void* (which gives off noisy compiler warnings thanks to -Wpmf-conversions)?
Related
#define MYSQLPP_MYSQL_HEADERS_BURIED
#include <mysql++/mysql++.h>
#include <iostream>
#include "/home/sulli313/Project4/Film.h"
void Film::showList(){
std::cout << "\n-----------------------------------" << std::endl;
std::cout << " Query Application " << std::endl;
std::cout << "-----------------------------------" << std::endl;
std::cout << " 1 All letter name actors" << std::endl;
std::cout << " 2 First # PG-13 and Above" << std::endl;
std::cout << " 3 All active/inactive users by store" << std::endl;
std::cout << " 4 Actor Movie titles and ids" << std::endl;
std::cout << "-1 Exit" << std::endl;
std::cout << "-----------------------------------" << std::endl;
std::cout << ">> Enter your choice:" << std::endl;
}
void Film::showOne(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
// Create a query
mysqlpp::Query query = myDB.query();
std::cout << "Please enter a Letter A-Z!" << std::endl;
std::string letter;
std::cin >> letter;
///////////////////////////////////Do this part/////////////////////////////////////////////
//if(letter)
query << "SELECT first_name, last_name "
<< "FROM actor "
<< "WHERE first_name "
<< "LIKE '" + letter + "%'";
// << "WHERE code = \"IST\"";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
std::cout << "--First/Last Names of Actors Whos First ";
std::cout << "Name Stars With " + letter + "--\n" << std::endl;
std::cout << std::left << std::setw(12) << "First Name" <<
std::setw(10) << "Last Name" << std::endl;
std::cout << "------------------------" << std::endl;
for (const auto & row : result) {
std::cout << std::left << std::setw(12) << row[0].c_str();
std::cout << std::setw(5) << row[1] << std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
void Film::showTwo(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
//Selecting the second option's query
std::cout << "Please type a limit 1-30!" << std::endl;
std::string limit;
std::cin >> limit;
mysqlpp::Query query = myDB.query();
query << "SELECT title "
<< "FROM film "
<< "WHERE rating = 'PG-13' "
<< "OR rating = 'R' "
<< "OR rating = 'NC-17' "
<< "LIMIT " + limit + " ";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
std::cout << "--First "+ limit +" Titles PG-13 to NC-17--\n" << std::endl;
std::cout << std::left <<std::setw(20)<< "Title" << std::endl;
std::cout << "------------------------------" <<std::endl;
for (const auto & row : result) {
std::cout << std::left << std::setw(20) << row[0].c_str()<< std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
void Film::showThree(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
//bind variable
std::cout << "Please type 1 for active and 0 for inactive!" << std::endl;
std::string active;
std::cin >> active;
if(active != "1" && active != "0"){
std::cout << "Wrong!" << std::endl;
std::cout << "Please type 1 for active and 0 for inactive!" << std::endl;
std::cin >> active;
}
//Selecting the second option's query
mysqlpp::Query query = myDB.query();
query << "SELECT Count(*) "
<< "FROM customer "
<< "WHERE active = '"+ active + "' "
<< "GROUP BY store_id";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
if(active == "1"){
std::cout << "--Count of All Active Users Grouped by Store Id--\n" << std::endl;
std::cout << std::left <<std::setw(20)<< "Active User Ids" << std::endl;
std::cout << "--------------------" <<std::endl;
} else {
std::cout << "--Count of All Inactive Users Grouped by Store Id--\n" << std::endl;
std::cout << std::left <<std::setw(20)<< "Inactive User Ids" << std::endl;
std::cout << "--------------------" <<std::endl;
}
for (const auto & row : result) {
std::cout << std::left << std::setw(20) << row[0].c_str()<< std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
void Film::showFour(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
//bind variable
std::cout << "Please type an actor id that is 1-200!" << std::endl;
std::string act_id;
std::cin >> act_id;
//Selecting the second option's query
mysqlpp::Query query = myDB.query();
query << "SELECT film.title, film.film_id, film_actor.actor_id "
<< "FROM film, film_actor "
<< "WHERE film_actor.actor_id = "+ act_id +" "
<< "GROUP BY title "
<< "LIMIT 20";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
std::cout << "--First 20 Titles and IDs of Actor Id 25--\n" << std::endl;
std::cout << std::left <<std::setw(22)<< "Title" <<
std::setw(10)<< "Film_id" << std::setw(0)<<"Actor_id" << std::endl;
std::cout << "----------------------------------------" <<std::endl;
for (const auto & row : result) {
std::cout << std::left<<std::setw(22)<< row[0].c_str() << std::setw(10) << row[1] << std::setw(0) << row[2].c_str() << std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
#ifndef FILM_H
#define FILM_H
#include <iostream>
#include <string>
class Film {
public:
void showList();
void showOne();
void showTwo();
void showThree();
void showFour();
private:
};
#endif
// Copyright
// Purpose: Project 4
// Date 11/25/2022
// Author: Colton Sullivan
#define MYSQLPP_MYSQL_HEADERS_BURIED
#include <mysql++/mysql++.h>
#include <iostream>
#include "/home/sulli313/Project4/Film.h"
int main() {
int choice;
showList();
std::cin >> choice;
if (choice == -1) {
std::cout << "Bye!" << std::endl;
}
while (choice != -1) {
while ( choice > 4 || choice < -1 || choice == 0 ) {
std::cout << "The wrong choice!!!" << std::endl;
std::cout << "" << std::endl;
std::cout << "to continue, press enter...";
showList();
std::cin >> choice;
}
if ( choice == 1 ) {
showOne();
}
if ( choice == 2 ) {
showTwo;
}
if ( choice == 3 ) {
showThree;
}
if ( choice == 4 ) {
showFour;
}
std::cin >> choice;
if ( choice == -1 ) {
std::cout << "Bye!" << std::endl;
}
}
}
When trying to switch the original program to object oriented programming, I ran into the problem of the error "Invalid use of non-static member function" popping up for the showOne, showTwo, showThree, showFour and showList functions.
If there is a way to access the functions that are created in the Film.cpp/Film.h files and use them in the QueryApp.cpp file to run that as the main, please let me know.
I have tried switching it from Film::showOne, Film::showTwo...etc to showOne(); and Film::showOne(); but either the same Invalid use of non-static member function error will show or it will say that it has not be declared in this scope.
Your showSomething() functions are all member functions of the Film type. In particular, because they are not marked as static, they are non-static member functions. That means they operate on an instance of Film:
class C {
public:
void a(); // <- non-static member function
static void b(); // <- static member function
};
void foo() {
C::b(); // okay, doesn't need an object
C::a(); // not okay, needs an object
C object; // instance of C
object.a(); // okay
object.b(); // also okay
}
Typically you would give your object some state information or it raises the question why you have a non-static member function to begin with. If you don't need state, make them free functions (i.e., functions that are not member functions) or make them static.
In your case, I suppose it would be helpful to create a database connection in your Film's constructor so you can use the database member in all the functions that need a database connection to function.
Something like this:
class Film {
private:
mysqlpp::Connection myDB;
public:
Film() : myDB("x", "localhost", "y", "z") {}
// ...
};
I have following test example:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result of the code above is:
init 0x7ffed27c6450 0x56480bc1eeb0 5
value 0x7ffed27c6470 0x56480bc1eeb0 5
done 0x7ffed27c6450 0 0
Looks great:
Now, move to:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo2(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result is:
init 0x7ffccc93a5c0 0x56124b3a8eb0 5
rvalue_ref 0x7ffccc93a5c0 0x56124b3a8eb0 5
done 0x7ffccc93a5c0 0x56124b3a8eb0 5
My problem is:
For the 1st case, it is perfectly called by "move semantics", and as you see, the ownership of the vector has been transfered to the function parameter. Finally, at "done", the data is null to verify the the vector at main() no longer owns the vector.
Now to explicitly claim the parameter is "rvalue reference", as case 2. As you see, actually it is like "call by (l)reference".
How can I figure out it?
I'm having trouble understanding the (to me intricate) mechanisms executed behind the scenes in the following code example:
#include <utility>
#include <memory>
#include <iostream>
struct A {int a;};
struct B {int b;};
std::pair<std::shared_ptr<A>, std::unique_ptr<B>&> FuncA() {
std::shared_ptr<A> a = std::make_shared<A>();
std::unique_ptr<B> b = std::make_unique<B>();
a->a = 12; b->b = 13;
std::cout << "FuncA a: " << a.get() << std::endl;
std::cout << "FuncA b: " << b.get() << std::endl;
std::cout << "FuncA a.a: " << a->a << std::endl;
std::cout << "FuncA b.b: " << b->b << std::endl;
return {a,b};
}
void FuncC(std::pair<std::shared_ptr<A>, std::unique_ptr<B>&> input) {
std::cout << "FuncC a: " << input.first.get() << std::endl;
std::cout << "FuncC b: " << input.second.get() << std::endl;
std::cout << "FuncC a.a: " << input.first->a << std::endl;
std::cout << "FuncC b.b: " << input.second->b << std::endl;
}
void FuncB() {
auto ret = FuncA();
std::cout << "FuncB a: " << ret.first.get() << std::endl;
std::cout << "FuncB b: " << ret.second.get() << std::endl;
std::cout << "FuncC a.a: " << ret.first->a << std::endl;
std::cout << "FuncC b.b: " << ret.second->b << std::endl;
FuncC(ret);
}
int main(){
FuncB();
}
I've compiled the code with both GCC and Clang which give similar results:
FuncA a: 0xfeaec0
FuncA b: 0xfeaed0
FuncA a.a: 12
FuncA b.b: 13
FuncB a: 0xfeaec0
FuncB b: 0x7ffd1c8e4a00
FuncC a.a: 12
FuncC b.b: 479087264
FuncC a: 0xfeaec0
FuncC b: 0x406100
FuncC a.a: 12
FuncC b.b: 1449378512
As is clearly visible, the address of the std::unique_pointer reference (and of course also its value) are not the same as within FuncA, but the address and value of the std::shared_pointer are unchanged.
What's happening here, and what (if anything) could be done to make the reference-passing correct?
Is some form of copy-constructor being executed on the std::unique_ptr as a result of returning from FuncA?
std::pair<std::shared_ptr<A>, std::unique_ptr<B>&> FuncA() {
// ...
std::unique_ptr<B> b = std::make_unique<B>();
// ...
return {a,b};
}
A local std::unique_ptr<B> is created and a reference to it is returned as the second element in the pair. This is a dangling reference and is later accessed, giving the program undefined behaviour.
In C++ parlance, is there a name for a type of class that does not inherit from any other class, has no virtual member functions, and is used as-is?
No there is no term for a type that does not inherit from any other class and has no virtual member functions.
One reason that such classification isnt that useful is that private inheritance is an implementation detail. (Maybe you are refering to public inheritance only?)
The type_traits library has lots of traits to query the category of a given type. See the below example:
#include <type_traits>
#include <iostream>
struct Foo {};
int main() {
std::cout << std::is_trivial<Foo>::value << "\n";
std::cout << std::is_trivially_copyable<Foo>::value << "\n";
std::cout << std::is_standard_layout<Foo>::value << "\n";
std::cout << std::is_pod<Foo>::value << "\n";
std::cout << std::is_literal_type<Foo>::value << "\n";
std::cout << std::has_unique_object_representations<Foo>::value << "\n";
std::cout << std::is_empty<Foo>::value << "\n";
std::cout << std::is_polymorphic<Foo>::value << "\n";
std::cout << std::is_abstract<Foo>::value << "\n";
std::cout << std::is_final<Foo>::value << "\n";
std::cout << std::is_aggregate<Foo>::value << "\n";
std::cout << std::is_signed<Foo>::value << "\n";
std::cout << std::is_unsigned<Foo>::value << "\n";
std::cout << std::is_bounded_array<Foo>::value << "\n";
std::cout << std::is_unbounded_array<Foo>::value << "\n";
}
Live
Foo is a trivial type, it is standard layout and it is a POD. It is neither polymorphic nor abstract. If you want a type that is not meant to be inherited from then you could make it final (Foo is not final).
I'm just playing around a little bit with decltype and noticed the intellisense in VS 2012 is giving me a error. This is the first time I have encountered this and the code still compiled.
#include <iostream>
int func(int param)
{
std::cout << "IM BEING CALLED: " << param << std::endl;
return 0;
}
int main()
{
auto& y = func;
auto z = func;
decltype((func))& x = func;
decltype((func)) k = func; // function 'k' may not be initialized but compiles
func(9);
x(10);
y(11);
z(12);
k(13);
std::cout << std::endl;
std::cout << "Address of func: " << func << std::endl;
std::cout << "Address of x: " << x << std::endl;
std::cout << "Address of y: " << y << std::endl;
std::cout << "Address of z: " << z << std::endl;
std::cout << "Address of k: " << k << std::endl;
std::cin.get();
return 0;
}
This is not a major problem nor interesting for most people but I was just wondering if anyone knows the reason behind the error?
I was just wondering if anyone knows the reason behind the error
It's just a parsing bug. Nothing more, nothing less.